比较两个文本文件,第三个文件中的输出

时间:2018-05-03 16:18:03

标签: python

File 1.txt

File 2.txt

Output File Reference Image

我尝试了但是我以另一种方式得到了结果

`import re, glob, logging

来自itertools import izip

def FindDiff(Doc1,Doc2,Doc3):

doc1read = open (Doc1,'r')
doc2read = open (Doc2,'r')
doc3write = open (Doc3,'w+')
find_pattern = r'\b\d*\.\d+|\d+\b'
lineno = 1
lines1 = doc1read.readline()
lines2 = doc2read.readline()
while lines1:
    regex1 = re.findall(find_pattern, lines1)
    regex2 = re.findall(find_pattern, lines2)
    length = len(regex1)-1
    for x in range (0, length):
        if (regex1[x] != regex2[x]):
            doc3write.write("File 1 has "+lines1+"and File 2 has "+lines2+"at line -" + str(lineno) + "\n")

            print regex1
            print regex2
            break
    lineno = lineno+1
    lines1 = doc1read.readline()
    lines2 = doc2read.readline()
doc1read.close()
doc2read.close()
doc3write.close()

FindDiff(' D:/F1.txt',' D:/F2.txt',' D:/F3.txt')`

1 个答案:

答案 0 :(得分:0)

我不知道您是否必须使用python,但您可以按照以下命令执行以下操作:

$diff --suppress-common-lines dif1.txt dif2.txt |grep -e '[><]'|cut -c2-

保存输出,您可以执行以下操作:

$diff --suppress-common-lines D:/F1.txt D:/F2.txt |grep -e '[><]'|cut -c2- > D:/F3.txt