脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python去除文件中重复的行实例

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

2021-03-11 10:36yizhen_nlp Python

今天小编就为大家分享一篇python去除文件中重复的行实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

python去除文件中重复的行,我们可以设置一个一个空list,res_list,用来加入没有出现过的字符行!

如果出现在res_list,我们就认为该行句子已经重复了,可以再加入到记录重复句子的list中。

如下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- 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去除文件中重复的行实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yizhen_nlp/article/details/70340271

延伸 · 阅读

精彩推荐