python-字符串替换字典单词 Posted on 2017-05-30 Edited on 2017-12-14 In 默认分类 , 技术 把text中对应word_dic中的key替换成values 123456def 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)