你好,我已经在使用这段代码了,
我有两个文件standard.txt,new.txt
standard.txt具有:ABC123ABC003ABC004new.txt具有:ABC123ABC004
我能够显示文件中的差异,但是我对实际显示哪行具有差异感兴趣。如果有人可以帮忙看看,并举例说明我做错了什么,那将非常有帮助我的代码是:
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和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中的内置类名,并且(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'))