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'
)。但是当我有一个文件包含很多句子并运行代码时,它会删除所有文本。谁能知道哪里可能是问题