Python实现的txt文件去重功能示例
作者:人饭子 时间:2021-12-22 23:42:49
本文实例讲述了Python实现的txt文件去重功能。分享给大家供大家参考,具体如下:
# -*- coding:utf-8 -*-
#! python2
import shutil
a=0
readDir = "/Users/Administrator/Desktop/old.txt" #old
writeDir = "/Users/Administrator/Desktop/new.txt" #new
# txtDir = "/home/Administrator/Desktop/1"
lines_seen = set()
outfile = open(writeDir, "w")
f = open(readDir, "r")
for line in f:
if line not in lines_seen:
a+=1
outfile.write(line)
lines_seen.add(line)
print(a)
print('\n')
outfile.close()
print("success")
其中old.tx如下:
www.jb51.net
www.baidu.com
www.sina.com.cn
www.jb51.net
www.google.com
www.sohu.com
www.jb51.net
www.163.com
运行后new.txt内容如下:
www.jb51.net
www.baidu.com
www.sina.com.cn
www.google.com
www.sohu.com
www.163.com
PS:这里再为大家提供几款相关工具供大家参考使用:
在线去除重复项工具:
http://tools.jb51.net/code/quchong
在线文本去重复工具:
http://tools.jb51.net/aideddesign/txt_quchong
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/sinat_35360663/article/details/80414699
标签:Python,txt文件,去重
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python编程实现正则删除命令功能
2022-10-19 16:45:08
django中使用Celery 布式任务队列过程详解
2022-07-08 02:25:55
![](https://img.aspxhome.com/file/2023/9/112139_0s.png)
python如何发布自已pip项目的方法步骤
2023-01-22 01:17:36
Ewebeditor及fckeditork单引号的问题
2010-04-05 21:12:00
python实现对变位词的判断方法
2022-01-29 04:36:10
详解Ubuntu环境下部署Django+uwsgi+nginx总结
2021-06-15 06:23:43
ServerXMLHTTP的setTimeouts超时设置
2010-01-02 20:38:00
Python基础语法之容器详解
2022-01-07 23:20:19
![](https://img.aspxhome.com/file/2023/4/121704_0s.jpg)
css闭合浮动元素
2008-05-09 19:41:00
Python基于正则表达式实现计算器功能
2021-08-17 13:00:02
用正则表达式找出不包含连续字符串abc的单词
2010-03-02 20:28:00
对python产生随机的二维数组实例详解
2022-10-29 14:13:28
PHP的mysqli_sqlstate()函数讲解
2023-06-08 14:30:30
![](https://img.aspxhome.com/file/2023/6/55456_0s.png)
简单实现Standby SQL Server 数据库的方法
2009-10-23 09:26:00
![](https://img.aspxhome.com/file/UploadPic/200910/20091023181227153.jpg)
基于Django与ajax之间的json传输方法
2023-01-12 19:52:58
PHP抓取及分析网页的方法详解
2023-11-24 08:17:10
Python fileinput模块使用实例
2021-11-28 09:01:18
python 基于pygame实现俄罗斯方块
2021-07-22 15:47:23
![](https://img.aspxhome.com/file/2023/0/113050_0s.jpg)
python取余运算符知识点详解
2023-05-16 00:04:50
Python实现一个简单的毕业生信息管理系统的示例代码
2023-12-20 04:40:46
![](https://img.aspxhome.com/file/2023/1/72161_0s.jpg)