fi =open("论语-网络版.txt","r",encoding="utf-8")
fo =open("论语-提取版.txt","w",encoding="utf-8")
wflag =False#写标记
newline =[]#创建一个新的列表#文本文件是带有"\n"的长字符串for line in fi :#按行读入文件,此时line的type是strif"【"in line:#重置写标记
wflag =Falseif"【原文】"in line:#检验是否到了要写入的内容
wflag =Truecontinueif wflag ==True:
newline =[]
K =list(line)iflen(K)>1:#去除文本中的空行for i in K :#写入需要内容if i!=" ":
newline.append(i)
strlist ="".join(newline)#合并列表元素
newlines =str(strlist)#list转化成strfor D inrange(1,23):#删掉句中()
newlines = newlines.replace("({})".format(D),"")for P inrange(0,25):#删掉前面数值标题for A inrange(0,25):
newlines = newlines.replace("{}·{}".format(P,A),"")for A inrange(0,25):
newlines = newlines.replace("{}".format(A),"")
fo.write(newlines)
fo.close()
fi.close()