[英]Find differences between two files in python
I am working on a code that compares two text files in python and prints the differences between the two.我正在编写一个代码,用于比较 python 中的两个文本文件并打印两者之间的差异。 I was told to use sets.
我被告知要使用集合。 Is it also possible to have a dialogue box to choose the file, instead of manually inputting file names?
是否也可以有一个对话框来选择文件,而不是手动输入文件名? I am very beginner level at python, so if you could write out the code, I would really appreciate it.
我对python非常初级,所以如果你能写出代码,我将不胜感激。
File1.txt文件1.txt
hamburgers
potatoes
avocado
grapes
seaweed
File2.txt文件2.txt
cheeseburgers
potatoes
peanuts
grapes
seaweed
so I would want the code to print cheeseburgers, peanuts所以我想要代码打印芝士汉堡,花生
This is what I have but not sure if it's right:这是我所拥有的,但不确定它是否正确:
old_path = 'File1.txt'
new_path = 'File2.txt'
old_lines = file(old_path).read().split('\n')
new_lines = file(new_path).read().split('\n')
old_lines_set = set(old_lines)
new_lines_set = set(new_lines)
old_added = old_lines_set - new_lines_set
old_removed = new_line_set - old_lines_set
for line in old_lines:
if line in old_added:
print '-' , line.strip()
elif line in old_removed:
print '+' , line.strip()
for line in new_lines:
if line in old added:
print '-' , line.strip()
elif line in old_removed:
print '+' , line.strip ()
doc = open(filename, 'r')
doc1 = open(filename, 'r')
f1 = [x for x in doc.readlines()]
f2 = [x for x in doc1.readlines()]
diff = [line for line in f1 if line not in f2] # lines present only in f1
diff1 = [line for line in f2 if line not in f1] # lines present only in f2
doc.close()
doc1.close()
An easier solution, using the built-in set features:一个更简单的解决方案,使用内置的集合功能:
a = set(['hamburgers', 'potatoes', 'avocado', 'grapes', 'seaweed'])
b = set(['cheeseburgers', 'potatoes', 'peanuts', 'grapes', 'seaweed'])
a.difference(b)
b.difference(a)
The set.difference() function gives you set objects again, which you can process as you want. set.difference() 函数再次为您提供设置对象,您可以根据需要进行处理。 [I hope I am not solving homework problems for you...]
【希望我不是为你解决作业问题……】
Below Solution worked for me with my exact requirement.下面的解决方案符合我的确切要求。
f1=open((os.getcwd()) + "\\Test1.txt","r")
f2=open((os.getcwd()) + "\\Test2.txt","r")
for i, j in zip(f1, f2):
if i != j:
print(i.rstrip() + "\t" + j.rstrip())
f1.close()
f2.close()
Output:输出:
R[10]=0x8 R[10]=0x13
R[54]=0x6 R[54]=0x4
R[59]=0x18 R[59]=0x58
R[60]=0x3d R[60]=0x4c
R[126]=0x59 R[126]=0xbd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.