python

超轻量级php框架startmvc

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

更新时间:2020-06-09 10:48:01 作者:startmvc
本文实例讲述了Python实现的txt文件去重功能。分享给大家供大家参考,具体如下:#-*-coding:u

本文实例讲述了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 txt文件 去重