采用集合去重,在新文件里逐行写入,达成目的
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")