首页 > 其他 > 详细

同时替换掉多个字符串

时间:2018-01-15 16:16:47      阅读:259      评论:0      收藏:0      [点我收藏+]

基础没学牢,看到一篇大神的文章,http://blog.csdn.net/huludan/article/details/50925735 同时替换多个字符串

 

import re  
def multiple_replace(text, adict):  
    rx = re.compile(|.join(map(re.escape, adict)))  
    def one_xlat(match):  
        return adict[match.group(0)]  
    return rx.sub(one_xlat, text) 

text = "Larry Wall is the creator of Perl" 
adict = {  
    "Larry Wall" : "Guido van Rossum",  
    "creator" : "Benevolent Dictator for Life",  
    "Perl" : "Python",  
}  
print multiple_replace(text, adict) 

 

技术分享图片

 

同时替换掉多个字符串

原文:https://www.cnblogs.com/cymwill/p/8288345.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!