两个文件的差异检查器并显示不同的行

Difference checker of two files and display what line is different

你好,我有这段代码,我一直在努力,

我有两个文件standard.txt、new.txt

standard.txt 有: ABC123 ABC003 ABC004 new.txt 有: ABC123 ABC004

我能够显示文件中的差异,但我对实际显示哪一行有差异感兴趣。如果有人可以帮忙看一下,也许可以举例说明我做错了什么,那将非常有帮助 我的代码是:

def open_file_and_return_list(file_path):
    list = []
    with open(file_path, 'r') as f:
        line = f.readline()
        while line:
            list.append(line)
            line = f.readline()
    return list

def clean_new_line(list):
    for i in range(len(list)):
        if "\n" in list[i]:
            list[i] = list[i].replace("\n", "")
    return list


if __name__ == "__main__":
    list1 = open_file_and_return_list(r"C:\Users\a\a\b\file_compare\new.txt")
    list2 = open_file_and_return_list(r"C:\Users\a\a\b\file_compare\standard.txt")
    list1 = clean_new_line(list1)
    list2 = clean_new_line(list2)
    diff = []
    for obj in list1:
        if obj not in list2:
            diff.append(obj)
    for obj in list2:
        if obj not in list1:
            diff.append(obj)

    print(diff)

    diff_file = input("\nINFO: Select what to name the difference(s) : ")
    with open(diff_file, 'w') as file_out:
        for line in diff:
            file_out.write("** WARNING: Difference found in New Config:\n " + line + "\n")
            print("WARNING: Difference in file: " + line)

例如,我正在比较的文件是两个配置文件,所以差异可能会显示在不同的两行中,因此我不想将每个差异显示为 1、2、3,而是举例说明在第 105 行发现差异:*****差异***

也许我需要做点什么?

for i,lines2 in enumerate(hosts1):
if lines2 != lines1[i]:
    print "line ", i, " in hosts1 is different \n"
    print lines2
else:
    print "same"

并使用枚举?

enumerate and zip 是你的朋友。为了获得差异,我会做类似的事情:

# Make sure both lists of lines are same length (for zip)
maxl = max(len(list1), len(list2))                                          
list1 += [''] * (maxl - len(list1))                                         
list2 += [''] * (maxl - len(list2))                                         

for iline, (l1, l2) in enumerate(zip(list1, list2)):
    if l1 != l2:
        print(iline, l1, l2)

此外,(1) 永远不要使用 list 作为变量名,因为它是 python 中的内置 class 名称,并且 (2) 获取文件中的所有行都有一行:

lines = open('path_to_file').read().splitlines()

我能够通过混合多种您的方法来完成我想要的。谢谢!

def open_file_and_return_list(file_path):
    list = []
    with open(file_path, 'r') as f:
        line = f.readline()
        while line:
            list.append(line)
            line = f.readline()
    return list


def clean_new_line(list):
    for i in range(len(list)):
        if "\n" in list[i]:
            list[i] = list[i].replace("\n", "")
    return list


if __name__ == "__main__":
    list1 = open_file_and_return_list(r"new.txt")
    list2 = open_file_and_return_list(r"standard.txt")
    maxl = max(len(list1), len(list2))
    list1 += [''] * (maxl - len(list1))
    list2 += [''] * (maxl - len(list2))
    diff = []
    diff_file = input("\nINFO: Select what to name the difference(s) : ")
    open('diff.txt', 'w').close()

    for iline, (l1, l2) in enumerate(zip(list1, list2)):
        if l1 != l2:
            print(iline, l1, l2)
            print(iline, l1, l2, file=open('diff.txt', 'a'))