1. 程式人生 > >python學習 論語提取原文

python學習 論語提取原文

fi = open("論語-網路版.txt", "r", encoding="utf-8")
fo = open("論語-提取版.txt", "w")
wflag = False            #寫標記
for line in fi:
    if "【" in line:     #遇到【時,說明已經到了新的區域,寫標記置否
        wflag = False
    if "【原文】" in line:  #遇到【原文】時,設定寫標記為True
        wflag = True
        continue    
    if wflag == True:    #根據寫標記將當前行內容寫入新的檔案
        for i in range(0,25):
            for j in range(0,25):
                line = line.replace("{}·{}".format(i,j),"**")
        
        for i in range(0,10):
            line = line.replace("*{}".format(i),"")
        for i in range(0,10):
            line = line.replace("{}*".format(i),"")
        line = line.replace("*","")
        fo.write(line)
        
fi.close()
fo.close()