我有兩個文件,我需要這兩個文件中不同的行。 這兩行文件中的行不符合要求。獲取兩個文件中的差異
我嘗試使用下面的腳本
file1 = open("test1.txt","r")
file2 = open("test2.txt","r")
lines1 = hosts0.readlines()
for i,lines2 in enumerate(file2):
if lines2 != lines1[i]:
print ("line ", i, " in File2 is different \n")
print (lines2)
else:
print ("Its similar")
然而,這僅在這兩個文件中相同的行號線進行比較。
我的文件示例:
File1:
User 1 is Sam and PC in VLAN Trust
User10 is Tom and PC in VLAN Sales
Harry is User 6 and in VLAN Fin
File2:
Harry is User 6 and in VLAN Fin
User 1 is Sam and PC in VLAN Trust
User10 is Tom and PC in VLAN Sales
User20 is Donald and VLAN is Trust
我所要的輸出告訴我File1中缺少的線,有file2中。只要兩個文件之間的任何一行是常見的,而不考慮不同的行號,則不應將其列爲差異。
這些文件有多大?如果它們很容易適應內存,則可以簡單地使用[sets](https://docs.python.org/2/library/sets.html):'diff = set(file1).symmetric_difference(set(file2)) '這不會保留關於行號的信息。 –
嗨Lukas,這些文件大約是10 MB,您建議的命令列出了區別並且不知道哪個文件缺少這些行。但對我來說就足夠了,因爲只有一個文件在我的情況下缺少了幾行。感謝:-) – Zoro99