采用集合去重,在新文件里逐行写入,达成目的
old_file = "D:/testdata/memberId.txt" #old
result_file = "D:/testdata/memberId_new.txt" #new
lines_seen = set()
out_file = open(result_file, "w")
f = open(old_file, "r")
for line in f:
if line not in lines_seen:
out_file.write(line)
lines_seen.add(line)
out_file.close()
print("distinct_success")
本文介绍了一种使用Python进行文本去重的方法,通过读取原始文件并利用集合数据结构过滤重复行,最后将不重复的内容写入新文件。这种方法简单有效,适用于处理大量文本数据的去重需求。
94

被折叠的 条评论
为什么被折叠?



