加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 21:05:19 所属栏目:Python 来源:网络整理
导读:python去除文件中重复的行,我们可以设置一个一个空list,res_list,用来加入没有出现过的字符行! 如果出现在res_list,我们就认为该行句子已经重复了,可以再加入到记录重复句子的list中。 如下代码: # -*- coding: UTF-8 -*-#程序功能是为了完成判断文件

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

您可能感兴趣的文章:

  • python去掉空白行的多种实现代码
  • python 自动去除空行的实例

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读