2017-01-11 175 views
-1

根據我的要求,我希望在Windows平臺上逐行匹配Python中的兩個文本文件。例如我有以下的文本文件:如何使用Python逐行匹配兩個純文本文件

文件1:

我叫XXX

命令成功完成。

我母親的名字是YYY

我的手機號碼是12345

的重型卡車撞向大樓在午夜

貨車吃在教師一個紅蘋果

文件2 :

我的名稱是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() 

如果有人能幫助做這個匹配,我會非常感激。

+0

如果你只是想檢查文件是否相同,提供的答案應該足夠。對於任何更復雜的情況,我建議你看一下https://docs.python.org/3.5/library/difflib.html –

+0

這個比較的結果是什麼?這裏的預期產出是多少? –

+0

它看起來像你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。展示這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ](http://stackoverflow.com/tour)和[如何提問](http://stackoverflow.com/questions/how-to-ask)。 – TigerhawkT3

回答

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:這會檢查文件是否相同。如果你想要一個邏輯比較的東西,你必須先做一些研究。

+0

對於不夠清晰,我表示歉意,所以我的問題是如何將腳本電影與它的字幕對齊,我在Python中編寫了以下代碼,但它不足以從兩個文本文件。 (你可以在編輯的問題中找到代碼) –

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") 

希望有幫助。

+0

我爲不夠清楚而抱歉,所以我的問題是如何將腳本電影與它的字幕對齊,我在Python中編寫了以下代碼,但它不足以從兩個文本文件。 (你可以在編輯的問題中找到代碼) –