【问题标题】:How to compare two text files and print the differences in python?如何比较两个文本文件并打印python中的差异?
【发布时间】:2021-10-03 22:51:12
【问题描述】:

我需要获取两个文本文件并比较它们以查看它们是否相同或不同。如果它们相同,则输出“Yes”,如果它们不同,则输出“No”。它还将打印出两个文本文件之间的差异。例如,如果一个文本文件有这样的:

  • 世界你好!
  • abc

另一个文本文件有这个:

  • 世界你好!
  • xyz

那么输出将是:

  • 没有
  • abc
  • xyz
firstFile = input("Enter the first file name: ")
secondFile = input("Enter the second file name: ")

f1 = open(firstFile,'r')
f2 = open(secondFile,'r')
i = 0

for line1 in f1:
    i += 1
      
    for line2 in f2:
          
        # matching line1 from both files
        if line1 == line2:  
            # print IDENTICAL if similar
            print("Yes")       
        else:
            print("Line ", i, ":")
            # else print that line from both files
            print("\tFile 1:", line1, end='')
            print("\tFile 2:", line2, end='')

【问题讨论】:

  • 如果行相同但上面的代码输出“是”并且注释说打印“相同”,则说明说输出应该是“否”。请澄清正确的输出。

标签: python compare text-files


【解决方案1】:

如果您想依次迭代 2 个文件并比较每一行,那么您可以在单个循环内对两个文件使用 readLine()。这将比较给定行上的完全匹配。

如果一个文件的行数少于另一个文件,您可能想也可能不想考虑该怎么做。

你可以这样做:

with open(firstFile, "r") as f1, open(secondFile, "r") as f2:
    line = 0
    while True:
        line += 1
        s1 = f1.readline()
        s2 = f2.readline()
        if len(s1) == 0:
            # file1 is at end
            if len(s2) != 0:
                # file2 is not at the end
                print(f"Line {line}:")
                print(s2)
            break
        if len(s2) == 0:
          # file1 has data but file2 is at end
          print(f"Line {line}:")
          print(s2)
          break
        if s1 == s2:
            print("IDENTICAL ")
        else:
            print(f"Line {line}:")
            print(s1, end='')
            print(s2, end='')

【讨论】:

    【解决方案2】:

    您也可以使用zip 来遍历两个文件:

    with open("test1.txt", "r") as left_file, open("test2.txt", "r") as right_file:
            
        differences = []
        
        for left, right in zip(left_file, right_file):
            
            if left != right:
                differences.append((left.rstrip(), right.rstrip()))
        
        print("No" if differences else "Yes")
        
        for difference in differences:
            
            print(difference[0])
            print(difference[1])
    

    我刚刚学到的东西,我喜欢传授...

    如果你只需要比较它们是否相同,那么你可以使用filecmp:

    is_identical = filecmp.cmp("test1.txt", "test2.txt", shallow=False)
    

    另一个有趣的包是difflib。它允许您使用不同的差异方法。下面是统一差异的示例:

    import difflib
    
    with open("test1.txt", "r") as left, open("test2.txt", "r") as right:
        
        differences = difflib.unified_diff(left.readlines(), right.readlines())
            
        for difference in differences:  # difference is empty if no differences
            print(difference)
    

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 2012-02-18
      • 2020-10-03
      • 1970-01-01
      • 2013-06-17
      • 2020-08-01
      • 1970-01-01
      • 2019-05-15
      • 2019-08-03
      相关资源
      最近更新 更多