根據我的要求,我希望在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()
如果有人能幫助做這個匹配,我會非常感激。
如果你只是想檢查文件是否相同,提供的答案應該足夠。對於任何更復雜的情況,我建議你看一下https://docs.python.org/3.5/library/difflib.html –
這個比較的結果是什麼?這裏的預期產出是多少? –
它看起來像你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。展示這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ](http://stackoverflow.com/tour)和[如何提問](http://stackoverflow.com/questions/how-to-ask)。 – TigerhawkT3