python去除文件中重复的行实例

2018-10-15 18:10

阅读:605

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) 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去除文件中重复的行实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。


评论


亲,登录后才可以留言!