0
我有一個文本文件包含一個關於故事的文本,我想找到一個單詞「like」並獲取它後面的下一個單詞,並調用一個函數來查找該單詞的同義詞。這裏是我的代碼:無法正確保存文件
file = 'File1.txt'
with open(file, 'r') as open_file:
read_file = open_file.readlines()
output_lines = []
for line in read_file:
words = line.split()
for u, word in enumerate(words):
if 'like' == word:
next_word = words[u + 1]
find_synonymous(next_word)
output_lines.append(' '.join(words))
with open(file, 'w') as open_file:
open_file.write(' '.join(words))
我的,我認爲在文本中唯一的問題,因爲當我寫一個句子包括單詞(像)它的工作原理(for example 'I like movies'
)。但是當我有一個文件包含很多句子並運行代碼時,它會刪除所有文本。誰能知道哪裏可能是問題