比较两个文件.txt-Python
你能帮我吗 我有两个文件.txt,它们之间存在差异,我需要对它们进行比较,并说明差异是什么以及在哪里 我做了一个简单的代码,但仍然没有回答我 文件1:比较两个文件.txt-Python,python,compare,Python,Compare,你能帮我吗 我有两个文件.txt,它们之间存在差异,我需要对它们进行比较,并说明差异是什么以及在哪里 我做了一个简单的代码,但仍然没有回答我 文件1: 123 1234 12345 123456 1234567 12345678 123456789 1234567890 saída do arquivo 123 1234 12345 123456 1234567 12345678 123456789 1234567890 fim de arquivo
123
1234
12345
123456
1234567
12345678
123456789
1234567890
saída do arquivo
123
1234
12345
123456
1234567
12345678
123456789
1234567890
fim de arquivo
文件2:
123
1234
12345
123456
1234567
12345678
123456789
1234567890
saída do arquivo
123
1234
12345
123456
1234567
12345678
123456789
1234567890
fim de arquivo
程序应返回:
我的代码:
test_lines = open('teste1.txt').readlines()
correct_lines = open('teste2.txt').readlines()
for test, correct in zip(test_lines, correct_lines):
if test != correct:
print (("Oh no! Expected %r; got %r." )% (correct, test));
break
else:
len_diff = len(test_lines) - len(correct_lines)
if len_diff > 0:
print ("Test file had too much data.")
elif len_diff < 0:
print ("Test file had too little data.")
else:
print ("Everything was correct!")
lista_final = list(set(test_lines) - set(correct_lines))
print(lista_final)
在您的文件中,第二个条目后有额外的空格和制表符 正如你在截图中看到的。
这就是为什么您会得到意想不到的结果欢迎来到SO-我怀疑您是否有一个定义良好的特征,说明您的算法应该返回什么样的差异。当arquivo在两个文件中相等时,为什么它是返回差异的一部分?或者,程序是否应该返回上发现的任何差异的每一行的所有其余部分?在我看来,你应该为自己和你在这里询问的人澄清你的要求。