私の要件に従って、Windows プラットフォームの Python で 2 つのテキスト ファイルを 1 行ずつ照合したいと考えています。たとえば、次のテキスト ファイルがあります。
ファイル1:
私の名前はxxxです
コマンドが正常に完了しました。
私の母の名前はyyyです
私の携帯電話番号は 12345 です
大型トラックが真夜中に建物に衝突した
大型トラックが学部で赤いリンゴを食べる
ファイル 2:
私の名前はxxxです
指図 。成功しました。
私の母の名前は
建物に衝突したのはなんて重いトラックなんだ
トラックは学部でリンゴを食べる
十分に明確でなくて申し訳ありませんが、私の問題は、スクリプト ムービーをその字幕に合わせる方法です。Python で次のコードを書きますが、2 つのテキスト ファイルから位置合わせを取得するには不十分です。
# 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()
誰かがこのマッチングを手伝ってくれるなら、私はとても感謝しています.