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

yipeiwu_com6年前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')
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设计】。

相关文章

pytorch中torch.max和Tensor.view函数用法详解

torch.max() 1. torch.max()简单来说是返回一个tensor中的最大值。 例如: >>> si=torch.randn(4,5) >&g...

django启动uwsgi报错的解决方法

django启动uwsgi报错的解决方法

uwsgi介绍 uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。 要注意 W...

python画图--输出指定像素点的颜色值方法

如下所示: # -*- coding: utf-8 -*- #------------------------------------------------------------...

pytorch对可变长度序列的处理方法详解

pytorch对可变长度序列的处理方法详解

主要是用函数torch.nn.utils.rnn.PackedSequence()和torch.nn.utils.rnn.pack_padded_sequence()以及torch.nn...

python 字典访问的三种方法小结

定义字典 dic = {'a':"hello",'b':"how",'c':"you"} 方法一: for key in dic:   print key,dic[key]   ...