本帖最后由 aloha20200628 于 2024-5-19 18:38 编辑
回复 1# zhengwei007
用jscript和python两个版本分别测试了楼主提供的示例文件》text.txt(ansi编码), 字典.txt(ansi/gb2312编码)
前者用时约50秒,后者用时约20秒,测试系统硬件指标是intel i7-5500U
以下代码存为test.bat运行,生成替换结果文件为 text.new.txt(ansi/gb2312编码)- @set @v=1 //&(cscript /e:jscript "%~f0")&exit/b
- //
- fso = new ActiveXObject('Scripting.FileSystemObject');
- fp = fso.OpenTextFile('字典.txt'), linesD = fp.readall().split('\r\n'), fp.close();
- fp = fso.OpenTextFile('text.txt'), lineT = fp.readall(), fp.close();
- fp = fso.OpenTextFile('text.new.txt',2,true);
- for (var kv,re,i=0,l=linesD.length; i<l; i++) {
- kv = linesD[i].split(','), k = trim(kv[0]), v = trim(kv[1]);
- eval('re=/'+k+'/gi;'), lineT = lineT.replace(re, v);
- }
- fp.write(lineT), fp.close(), WSH.quit();
- //
- function trim (s) { return s.replace(/(^\s*)|(\s*$)/g,''); }
复制代码 如果楼主已经预装了python系统,可将以下代码存为test.py,直接在命令行运行,生成替换结果文件为 text.new.txt(ansi/gb2312编码)- #encoding=gbk
- import re
- with open('text.txt', 'r', -1) as fr:
- tLine = fr.read()
- with open('字典.txt', 'r', -1) as fr:
- dLines = fr.readlines()
- for d in dLines:
- kv = d.split(',')
- k, v = kv[0].strip(), kv[1].strip()
- tLine = re.sub(k, v, tLine, flags=re.I|re.S)
- with open('text.new.txt', 'w', -1) as fw:
- fw.write(tLine)
复制代码
|