如何使用Python逐行匹配两个纯文本文件

时间:2017-01-11 10:35:20

标签: python text matching textmatching

根据我的要求,我希望在Windows平台上逐行匹配两个文本文件。例如,我有以下文本文件:

File1中:

  

我的名字是xxx

     

命令成功完成。

     

我母亲的名字是yyy

     

我的手机号码是12345

     这辆重型卡车在午夜时分坠入大楼

     卡车在教员里吃了一个红苹果

File2:

  

我的名字是xxx

     

命令。成功。

     

我母亲的名字是

     是什么重型货车撞到了建筑物里

     卡车在教职员工吃了一个苹果

我为不够清楚而道歉,所以我的问题是如何将脚本电影与其字幕对齐,我在Python中编写以下代码,但这还不足以从两个文本文件中获得对齐:

 # Open file for reading in text mode (default mode)
f1 = open('F:/CONTRIBUTION 2017/SCRIPT-SUBTITLES CODES/Script Alignement Papers/f1.txt','r')
f2 = open('F:/CONTRIBUTION 2017/SCRIPT-SUBTITLES CODES/Script Alignement Papers/f2.txt','r')

#Print confirmation
# print("-----------------------------------")
#print("Comparing files ", " > " + fname1, " < " +fname2, sep='\n')
# print("-----------------------------------")

# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()

# Initialize counter for line number
line_no = 1

# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':

    # Strip the leading whitespaces
    f1_line = f1_line.rstrip()
    f2_line = f2_line.rstrip()

    # Compare the lines from both file
    if f1_line != f2_line:

        # If a line does not exist on file2 then mark the output with + sign
        if f2_line == '' and f1_line != '':
            print("=================================================================")
            print("=================================================================")
            print("line does not exist on File 2 ====================")
            print("=================================================================")
            print(">+", "Line-%d" % line_no, f1_line)
        # otherwise output the line on file1 and mark it with > sign
        elif f1_line != '':

            print("=================================================================")
            print("=================================================================")
            print("otherwise output the line on file1 ====================")
            print("=================================================================")
            print(">", "Line-%d" % line_no, f1_line)

        # If a line does not exist on file1 then mark the output with + sign
        if f1_line == '' and f2_line != '':
            print("=================================================================")
            print("=================================================================")
            print("=line does not exist on File 1 ====================")
            print("=================================================================")
            print("<+", "Line-%d" % line_no, f2_line)
        # otherwise output the line on file2 and mark it with < sign
        elif f2_line != '':
            print("=================================================================")
            print("=================================================================")
            print("otherwise output the line on file2 ====================")
            print("=================================================================")
            print("<", "Line-%d" %  line_no, f2_line)

        # Print a blank line
        print()

    #Read the next line from the file
    f1_line = f1.readline()
    f2_line = f2.readline()


    #Increment line counter
    line_no += 1

# Close the files
f1.close()
f2.close()

如果有人能帮忙做这个匹配,我将非常感激。

2 个答案:

答案 0 :(得分:0)

发布您尝试编写的代码会很好。这感觉就像我们正在做功课,让你看起来很懒。话虽如此,请看看以下内容:

with open(file1, 'r') as f1, open(file2, 'r') as f2:
    if f1.readlines() == f2.readlines():
        print('Files {} & {} are identical!'.format(file1, file2))

PS:检查文件是否相同。如果你想要一个像逻辑比较的东西,你必须先做一些研究。

答案 1 :(得分:0)

一种可能的方法是将文件的行存储在列表中,然后比较列表。

lines_of_file1 = []
file = open("file1.txt","r")
line = 'sample'
while line != '':
    line = file.readline()
    lines_of_file1.append(line)
file.close()
lines_of_file2 = []
file = open("file2.txt","r")
line = 'sample'
while line != '':
    line = file.readline()
    lines_of_file2.append(line)
file.close()
same = True
for line1 in lines_of_file1:
     for line2 in lines_of_file2:
        if line1 != line2:
            same = False
            break
if same:
    print("Files are same")
else:
    print("Files are not same")

希望有所帮助。