【发布时间】:2018-02-09 07:23:10
【问题描述】:
我有两个文本文件 1.txt 是单词词典,另一个是带有短语的 2.txt 现在我想检查 1.txt 和 2.txt 中的常用词,我想用这些常用词替换第三个词“解释”。
我尝试了很多破解方法,但都失败了。谁能帮帮我
我用过的代码:
wordsreplace = open("1.txt",'r')
with open("2.txt") as main:
words = main.read().split()
replaced = []
for y in words:
if y in wordreplace:
replaced.append(wordreplace[y])
else:
replaced.append(y)
text = ' '.join(replaced)
replaced = []
for y in words:
replacement = wordreplace.get(y, y)
replaced.append(replacement)
text = ' '.join(replaced)
text = ' '.join(wordreplace.get(y, y) for y in words)
new_main = open("2.txt", 'w')
new_main.write(text)
new_main.close()
这段代码写了 2.txt 但我无法替换文字
【问题讨论】:
-
如果您发布该文件内容和预期结果会更好