python去除文件中重復的行,我們可以設置一個一個空list,res_list,用來加入沒有出現過的字符行!
如果出現在res_list,我們就認為該行句子已經重復了,可以再加入到記錄重復句子的list中。
如下代碼:
# -*- coding: UTF-8 -*-#程序功能是為了完成判斷文件中是否有重復句子#并將重復句子打印出來 res_list = []#f = open('F:/master/master-work/code_of_graduate/LTP_data/raw_plain.txt','r')f = open('F:/master/master-work/code_of_graduate/chu_li_shuju/ldc-weibo-train-res.txt','r')res_dup = []index = 0file_dul = open('F:/master/master-work/code_of_graduate/chu_li_shuju/ldc-weibo-train-dul.txt', 'w')for line in f.readlines(): index = index + 1 if line in res_list: temp_str = "" temp_str = temp_str + str(index) #要變為str才行 temp_line = ''.join(line) temp_str = temp_str+temp_line #最終要變為str類型 file_dul.write(temp_str); #將重復的存入到文件中 else: res_list.append(line)
以上這篇python去除文件中重復的行實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答