python 将字符串中的多个模式替换为提供的字典中的模式。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将字符串中的多个模式替换为提供的字典中的模式。相关的知识,希望对你有一定的参考价值。

import re

def multisub(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)

以上是关于python 将字符串中的多个模式替换为提供的字典中的模式。的主要内容,如果未能解决你的问题,请参考以下文章