我有两个文本文件,file1.txt和file2.txt,我需要比较并删除重复的行。这两个文件的大小不相同。
我已尝试使用filecmp
和openfiles
,但它们不起作用。
with open('crones.txt', 'r') as file1:
with open('destino.txt', 'r+') as file2:
lineas = file1.readlines()
same = set(file1).intersection(file2)
file2.close()
file1.close()
#file1 = open("crones.txt","w")
#for linea in lineas:
# if linea!=same+"\n":
# f.write(linea)
print same
print lineas
#same.discard('\n')
#with open('some_output_file.txt', 'w') as FO:
# for line in same:
# FO.write(line)
答案 0 :(得分:0)
这可能是您正在寻找的内容:
with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2:
a = f1.readlines()
b = f2.readlines()
non_duplicates = [line for line in a if line not in b]
non_duplicates += [line for line in b if line not in a]
non_duplicates 现在是一个列表,其中包含每个文件中不会出现在两个文件中的所有行。如果您还担心每个文件中的重复项,可以添加以下内容:
non_duplicates = list(set(non_duplicates))
其他一些说明: