0%

python-字符串替换字典单词

把text中对应word_dic中的key替换成values

1
2
3
4
5
6
def replace_words(text, word_dic):
yo = re.compile('|'.join(map(re.escape, word_dic)))

def translate(mat):
return word_dic[mat.group(0)]
return yo.sub(translate, text)