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设计】。

相关文章

Python找出微信上删除你好友的人脚本写法

相信大家在微信上一定被上面的这段话刷过屏,群发消息应该算是微信上流传最广的找到删除好友的方法了。但群发消息不仅仅会把通讯录里面所有的好友骚扰一遍,而且你还得挨个删除好几百个聊天记录,回复...

python装饰器与递归算法详解

1、python装饰器 刚刚接触python的装饰器,简直懵逼了,直接不懂什么意思啊有木有,自己都忘了走了多少遍Debug,查了多少遍资料,猜有点点开始明白了。总结了一下解释得比较好的,...

python中的线程threading.Thread()使用详解

python中的线程threading.Thread()使用详解

1. 线程的概念: 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元。一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈...

Django 模型类(models.py)的定义详解

Django 模型类(models.py)的定义详解

一. #在models.py中添加 #代码如下 from django.db import models #出版商 class Publisher(models.Model):...

python 简单的多线程链接实现代码

服务端: #!/usr/bin/env import SocketServer class myMonitorHandler(SocketServer.BaseRequestHand...