我有一个程序比较两个文本文件并将差异打印到一个新的文本文件,但我想修改它,所以它只打印出第一个差异出现的行和列。以下是我到目前为止的情况:
f1 = open("file1.txt", "r")
f2 = open("file2.txt", "r")
fileOne = f1.readlines()
fileTwo = f2.readlines()
f1.close()
f2.close()
outFile = open("file3.txt", "w")
x = 0
for i in fileOne:
if i != fileTwo[x]:
outFile.write(i+" <> "+fileTwo[x])
x += 1
outFile.close()
感谢。
答案 0 :(得分:1)
第一场比赛后只需break
。
for i in fileOne:
if i != fileTwo[x]:
outFile.write(i+" <> "+fileTwo[x])
break
x += 1
答案 1 :(得分:0)
with open("file1.txt", "r") as f1:
lines1 = f1.readlines()
with open("file2.txt", "r") as f2:
lines2 = f2.readlines()
bound = max(len(lines1), len(lines2))
for idx, line1, line2 in zip(xrange(bound), lines1, lines2):
if line1 != line2:
diff_line = idx
break
bound = max(len(line1), len(line2))
for idx, c1, c2 in zip(xrange(bound), list(line1), list(line2)):
if c1 != c2:
diff_char = idx
break
print "first difference is on line {0:g} and character {1:g}".format(diff_line, diff_char)
答案 2 :(得分:0)
使用enumerate
和itertools
。此解决方案一次只会将两行(每行一行)加载到内存中。
from itertools import izip, dropwhile, izip_longest
with open("abc1") as f1, open("abc2") as f2:
for i, (x, y) in enumerate(izip(f1, f2), start=1):
if x != y:
print "line number is", i
column =next(dropwhile(lambda(i, c1, c2):c1==c2,
((i, c1, c2) for i,(c1, c2) in enumerate(izip_longest(x, y)))))[0]
print "column number is", column
break