[英]Python - How to compare two files and output only the different lines in a third file
我在SO上搜索了類似的問題,但沒有找到對我有用的東西。
我有兩個大文件,它們應該相同,但是其中一個文件比另一個長60行。 我想知道這些線是什么以及在哪里可以找到它們。
我讀過一個人可以使用difflib來做到這一點,但我不知道該怎么做。 我總是在文件中得到+
和-
,但是我不想要那樣。 我只想瀏覽兩個文件,並將不常見的60行報告到第三個文件中。
我寫了這段代碼,但沒有打印出不同的行。
f1 = open('file1.txt','r')
f2 = open('file2.txt','r')
f3 = open('file3.txt','w')
diff = set(f1).difference(f2)
same.discard('\n')
for line in same:
f3.write(line)
好吧,您可以執行以下操作:
with open('file1.txt') as infile:
f1 = infile.readlines()
with open('file2.txt') as infile:
f2 = infile.readlines()
only_in_f1 = [i for i in f1 if i not in f2]
only_in_f2 = [i for i in f2 if i not in f1]
with open('file3.txt', 'w') as outfile:
if only_in_f1:
outfile.write('Lines only in file 1:\n')
for line in only_in_f1:
outfile.write(line)
if only_in_f2:
outfile.write('Lines only in file 2:\n')
for line in only_in_f2:
outfile.write(line)
注意:不同行中的相同內容被視為差異
您可以使用集合輕松解決此問題。
set1 = set()
with open(file1) as f:
for line in f:
set1.add(line.strip())
#Repeat for set 2
with open(diff_file, 'w') as f:
for line in set2 - set1:
f.write(line + '\n')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.