<p>@nhahtdh提出的答案是正确的,但我认为比规范示例更少的pythonic,规范示例使用比regex操作更少不透明的代码,并利用了python的内置数据结构和匿名函数特性。</p>
<p>在这种情况下,翻译词典是有意义的。实际上,Python食谱就是这样做的,如本例所示(从ActiveState<a href="http://code.activestate.com/recipes/81330-single-pass-multiple-replace/" rel="noreferrer">http://code.activestate.com/recipes/81330-single-pass-multiple-replace/</a>复制)</p>
<pre><code>import re
def multiple_replace(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
if __name__ == "__main__":
text = "Larry Wall is the creator of Perl"
dict = {
"Larry Wall" : "Guido van Rossum",
"creator" : "Benevolent Dictator for Life",
"Perl" : "Python",
}
print multiple_replace(dict, text)
</code></pre>
<p>所以在你的例子中,你可以做一个dict<code>trans = {"a": "aa", "b": "bb"}</code>,然后把它和你想要翻译的文本一起传递到<code>multiple_replace</code>。基本上,该函数所做的就是创建一个包含要翻译的所有正则表达式的大型正则表达式,然后当找到一个正则表达式时,将lambda函数传递给<code>regex.sub</code>以执行翻译字典查找。</p>
<p>您可以在读取文件时使用此函数,例如:</p>
<pre><code>with open("notes.txt") as text:
new_text = multiple_replace(replacements, text.read())
with open("notes2.txt", "w") as result:
result.write(new_text)
</code></pre>
<p>实际上,我在制作中使用了这种精确的方法,在这种情况下,我需要将一年中的几个月从捷克语翻译成英语来完成一个网页抓取任务。</p>
<p>正如@nhahtdh指出的,这种方法的一个缺点是它没有前缀自由:作为其他字典键前缀的字典键将导致方法中断。</p>