必须检查2个文件:
我找到了一些有趣的代码片段,但它只比较并将新条目直接写入文件。我无法将其写入第三个文件。
有什么想法吗?
with open('./export/import.txt', 'r') as infile, open('./process/new.txt', 'w') as outfile:
seen = set()
for line in infile:
for word in line.split():
if word not in seen:
seen.add(word)
outfile.write('{}\n'.format(word))
答案 0 :(得分:3)
这应该有所帮助。
with open('./export/archive.txt', 'r') as archive, open('./export/import.txt', 'r') as importtxt, open('./export/new.txt', 'w') as outfile:
toCheck = archive.read()
for word in importtxt:
if word not in toCheck:
outfile.write('{}\n'.format(word))
这应该有所帮助。