Posted in Python onJuly 07, 2018
本文实例讲述了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如下:
3water.com
www.baidu.com
www.sina.com.cn
3water.com
www.google.com
www.sohu.com
3water.com
www.163.com
运行后new.txt内容如下:
3water.com
www.baidu.com
www.sina.com.cn
www.google.com
www.sohu.com
www.163.com
Python实现的txt文件去重功能示例
- Author -
人饭子声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@