你那个文件太不标准了,有重复的,词条有三行、有四行的。
2 I, u) v" \9 n# b$ W" W光替换是不能解决问题的,脚本代码和转换结果都给你~
复制内容到剪贴板
代码:
# -*- coding: cp936 -*-
import re
prog=re.compile("^\d{2} 《")
f=open("Full CH-ENCY.txt","rU")
t=open('t.txt','w')
entry=""
dic={}
for line in f:
line=line.rstrip()#去除换行符
if line:
if entry=="":
entry=line
if entry not in dic:
dic[entry]=[]#合并词条
elif prog.match(line):
if line not in dic[entry]:#防止重复
dic[entry].append(line)
entry=""#词条结束
else:
if line not in dic[entry]:
dic[entry].append(line)
for i in sorted(dic.keys()):
print >>t,"%s\t%s"%(i,r"\n".join(dic))#写入文件
t.close()[
本帖最后由 osfans 于 2008-10-8 00:36 编辑 ]