【问题标题】:Replace words that are common in two text file with third word用第三个单词替换两个文本文件中常见的单词
【发布时间】: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 但我无法替换文字

【问题讨论】:

  • 如果您发布该文件内容和预期结果会更好

标签: python replace


【解决方案1】:

我不想指出你代码中的问题,因为这个任务基本上几行就可以完成。这是一个独立的示例(没有文件,只有文本输入)

  • 首先创建一个名为wordsset,您可以在需要时查找(将read().split() 传递给set() 将在从文件中完成:words = set(main.read().split())
  • 现在使用词边界词正则表达式替换函数

如果在字典中找不到单词,替换函数发出“解释”:

words = {"computer","random"}

text = "computer sometimes yields random results"

import re

new_text = re.sub(r"\b(\w+)\b",lambda m : "explain" if m.group(1) in words else m.group(1),text)

print(new_text)

所以替换由正则表达式引擎处理,当匹配时调用我的lambda,所以我可以决定是替换这个词,还是重新发出它。

结果:

explain sometimes yields explain results

当然,这不处理复数(计算机,...),它也必须在字典中。

【讨论】:

    猜你喜欢
    • 2015-03-02
    • 2015-01-30
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 2016-08-19
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多