如何判断一个字符串是否比另一个字符串长,然后打印出不匹配的地方?

How to determine if one string is longer than another, and then print out where the mismatch occurs?

我正在编写一个程序,它接受两个用户输入的文件并确定两者之间是否存在任何不匹配。如果一个文件比另一个长,我想打印出较短的文件中没有字符可以在该索引处与较长的文件进行比较。截至目前,我的程序遍历两个文件并打印出现不匹配的地方,但一旦到达较短文件的末尾就会停止。

我的代码如下:

def character_difference(userfile1, userfile2):
    opened_file_1 = open(userfile1)
    opened_file_2 = open(userfile2)
    f1 = opened_file_1.read(-1)
    f2 = opened_file_2.read(-1)
    for index, (char1, char2) in enumerate(zip(f1, f2)):
        if char1 != char2:
            print("Mismatch at character", index, "%s != %s" % (char1, char2))
        else:
            continue
    opened_file_1.close()
    opened_file_2.close()
def main():
    userfile1 = input("Enter the name of the first file: ")
    userfile2 = input("Enter the name of the second file: ")
    character_difference(userfile1, userfile2)

main()

我将如何添加打印语句,指出空 space 所在的位置没有字符?我不确定如何继续枚举其余的较长字符串。

zip 只会迭代到传递给它的可迭代对象中的最短者。引用文档,

The iterator stops when the shortest input iterable is exhausted.

相反,使用 itertools.zip_longest 迭代直到最长的可迭代对象,并为较短的可迭代对象使用默认值。引用文档,

If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.

例如,

from itertools import zip_longest

with open(userfile1) as f1, open(userfile2) as f2:
  f1 = f1.read(-1)
  f2 = f2.read(-1)

for idx, (c1, c2) in enumerate(zip_longest(f1, f2, fillvalue="")):
  if c1 != c2:
    print("Mismatch at character", idx, "%s != %s" % (c1, c2))