python去除文件中重复的行实例
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=0
file_dul=open('F:/master/master-work/code_of_graduate/chu_li_shuju/ldc-weibo-train-dul.txt','w')
forlineinf.readlines():
index=index+1
iflineinres_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去除文件中重复的行实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短