Python实现的txt文件去重功能示例

为了把明天的工作做好,我们必须把今天的工作先做好了,不要给明天的工作找麻烦。在工作面前,态度决定一切。没有不重要的工作,只有不重视工作的人。不同的态度,成就不同的人生,有什么样的态度就会产生什么样的行为,从而决定不同的结果。

本文实例讲述了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.haodaima.com
www.baidu.com
www.sina.com.cn
www.haodaima.com
www.google.com
www.sohu.com
www.haodaima.com
www.163.com

运行后new.txt内容如下:

www.haodaima.com
www.baidu.com
www.sina.com.cn
www.google.com
www.sohu.com
www.163.com

PS:这里再为大家提供几款相关工具供大家参考使用:

在线去除重复项工具:
http://tools.haodaima.com/code/quchong

在线文本去重复工具:
http://tools.haodaima.com/aideddesign/txt_quchong

希望本文所述对大家Python程序设计有所帮助。

以上就是Python实现的txt文件去重功能示例。我不去想是否能够胜利,既然选择了远方,便只顾风雨兼程;我不去想,身后会不会袭来寒风冷雨,既然目标是地平线,留给世界的只能是背影!更多关于Python实现的txt文件去重功能示例请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战