python去除文件中重复的行实例
2018-06-29 14:51
1491 查看
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合并字典键值并去除重复元素的实例
- Python模糊查询本地文件夹去除文件后缀的实例(7行代码)
- python去除文件中重复的行
- 去除文件中的重复元素后写入文件 分类: python 小练习 open()文件读写 2013-12-03 11:21 356人阅读 评论(0) 收藏
- Python实现FTP上传文件或文件夹实例(递归)
- Python基本语法实例:文件处理,字符串转换
- python文件读写实例,超级方便
- python 去除 list 重复元素
- Python os模块实例之遍历目录及子目录指定扩展名的文件
- 10.python中如何从一个序列中去除重复项
- 【Python】一个python实例:给重要的文件创建备份.摘自crossin-python简明教程
- 实例详细说明linux下去除重复行命令uniq
- python之SocketServer传文件实例
- Python 中去除文件的只读属性
- Python判断文件和字符串编码类型的实例
- Python 去除列表中重复的元素
- 去除文件中的重复元素后写入文件
- Python列表list操作符实例分析【标准类型操作符、切片、连接字符、列表解析、重复操作等】