1、txt文件(和Python代码在同一目录):
D:\Python_Project\test\ori.txt,内容如下所示:
D:\Python_Project\test\gen.txt,内容为空:
2、代码test3.py:
# -*- coding: utf-8 -*-
num = 0
oriTxt = "ori.txt" # 包含重复字段的文本
genTxt = "gen.txt" # 去重后的文本
lines = set()
outfile = open(genTxt, "w")
with open(oriTxt, "r") as f:
for line in f:
if line not in lines:
num += 1
outfile.write(line)
lines.add(line)
outfile.close()
print("success,共生成" + str(num) + "条数据!")
3、运行:
4、验证:
D:\Python_Project\test\gen.txt,内容如下所示: