I'm currently having a problem when I run a script that I wrote to tell if two texts files are identical:
a="MyOut1.txt"
b="out1.txt"
f1=open(a,'r')
f2=open(b,'r')
l1=f1.readlines()
l2=f2.readlines()
f1.close()
f2.close()
print(l2==l1)
When I run the code via terminal I get a False result (even though I expect the files to be the same) and when I run it through python shell it gives me True as you can see:
答案 0 :(得分:0)
Are you 100% sure that both files are the same. Also are you sure that the script you wrote is exactly the same as the lines shown in the picture. Lastly two double check if python sees the files as the same do as Simon suggested and print(l1)
and print(l2)
.
A much quicker way to read from the files and test if they are the same is.
l1 = open("one.txt", "r").read()
l2 = open("two.txt", "r").read()
print(l2 == l2)
答案 1 :(得分:0)
我找到了为什么文本不相同的原因但我仍然需要回答为什么会发生这种情况: 当我打印两个文本的内容时它是相同的,但是当我将它作为列表打印时,似乎其中一个文本在每个\ n之前都包含\ r。
>>>
>>> l1[10]
'\n'
>>> l2[10]
'\r'
>>> l2[11]
'\n'
每当我在文本中得到\ n时,就会发生这种情况。
我的以下问题是:
a)为什么会发生这种情况?
b)我只需要一个选项来比较文本的内容。有没有办法做得更好?