Python字符串替换实例分析,python实例分析
Python字符串替换实例分析,python实例分析
本文实例讲述了Python字符串替换的方法。分享给大家供大家参考。具体如下:
单个字符替换
s = 'abcd' a = ["a", "b", "c"] b = ["c", "d", "e"] import string s.translate(string.maketrans(''.join(a),''.join(b))) print s
输出结果为:abcd
字符串替换,改善版
s = "hello, i'm mouren, hehe~~,hehe~~mourenmouren" a = ["mouren", "hehe"] b = ["mr", "hoho"] import re dic = dict(zip(a,b)) pattern = re.compile('(' + '|'.join(a) + ')') s = pattern.sub(lambda a:dic[a.group()], s) print s
输出结果为:hello, i'm mr, hoho~~,hoho~~mrmr
希望本文所述对大家的Python程序设计有所帮助。
相关内容
- Python解析nginx日志文件,
- Python生成不重复随机值的方法,python生成重复
- Python使用MYSQLDB实现从数据库中导出XML文件的方法,py
- python单元测试unittest实例详解,pythonunittest
- Python中DJANGO简单测试实例,pythondjango实例
- Python使用CMD模块更优雅的运行脚本,pythoncmd模块脚本
- python学习数据结构实例代码,python数据结构实例
- Python使用MONGODB入门实例,pythonmongodb实例
- Python中IPYTHON入门实例,pythonipython入门
- Python素数检测的方法,python素数检测
评论关闭