0
我想查找包含HTML代碼的2個txt文件之間的區別,嘗試了difflib模塊,但是無論我嘗試什麼,delta都保持爲0。 我需要找到有多少行不同於總HTML代碼查找2個HTML文件之間的不同線條
謝謝!
import difflib
count = 0
count2 = 0
delta = 0
f = open('C\html1.txt', 'r')
f2 = open('C\html2.txt', 'r')
for i in f2:
count2 += 1
for i in f:
count += 1
diff = difflib.udiff = difflib.unified_diff(
f.readlines(),
f2.readlines(),
fromfile='C\html1.txt',
tofile='C\html2.txt',
)
for line in diff:
delta +=1
print delta
print count
per = (delta * 100)/count
您是通過'F'迭代和DIFF之前移動文件指針到文件的開頭'f2'遞增'count'和'count2',使文件的位置結束。當執行readlines時,它們都是空的列表。做'f.seek(0)'和'f2.seek(0)'來重置文件的位置回到頂端 – Wondercricket