Python3.4-文本-替换字符串中的子串
2015-07-03 16:42
691 查看
"""
python版本: 3.4
替换字符串中的子串
"""
import string
info = """姓名: $name,
年龄: $age,
博客: $blog,
http://${weibo},
$$帅
"""
#string.Template(template)
info_template = string.Template(info)
#以字典的方式一一赋值
info_dic={'name':'毕小朋','age':30,'blog':'http://blog.csdn.net/wirelessqa','weibo':"www.weibo.com/wirelessqa"}
#substitute(mapping, **kwds)
print(info_template.substitute(info_dic))
"""
>>
姓名: 毕小朋,
年龄: 30,
博客: http://blog.csdn.net/wirelessqa, http://www.weibo.com,
$帅
"""
#转成字典后再赋值
info_dic2=dict(name='毕小朋',age=30,blog='http://blog.csdn.net/wirelessqa',weibo='www.weibo.com/wirelessqa')
print(info_template.substitute(info_dic2))
"""
>>
姓名: 毕小朋,
年龄: 30,
博客: http://blog.csdn.net/wirelessqa, http://www.weibo.com,
$帅
"""
#safe_substitute(mapping, **kwds)
#当我们少一个键(weibo被拿掉了哦)时,查看结果如何
test_safe_substitute=dict(name='毕小朋',age=30,blog='http://blog.csdn.net/wirelessqa')
try:
print(info_template.substitute(test_safe_substitute))
except KeyError:
print("error: 映射中没有weibo这个键")
"""
>>
error: 映射中没有weibo这个键
"""
#使用safe_substitute(mapping, **kwds)
print(info_template.safe_substitute(test_safe_substitute))
"""
>>
姓名: 毕小朋,
年龄: 30,
博客: http://blog.csdn.net/wirelessqa, http://${weibo},
$帅
"""
#locals()提供了基于字典的访问局部变量的方式
info = string.Template('老毕是$shuai,芳龄$age')
shuai='帅哥'
age=18
print(info.substitute(locals())) #>>老毕是帅哥,芳龄18
#使用关键字作为参数替换
info = string.Template('老毕喜欢年龄$age的$who')
for i in range(18,39):
print(info.substitute(age=i,who='美女'))
"""
>>
老毕喜欢年龄18的美女
老毕喜欢年龄19的美女
老毕喜欢年龄20的美女
老毕喜欢年龄21的美女
....
老毕喜欢年龄38的美女
"""
#同时使用关键字参数和字典
for age in range(18,39):
print(info.substitute(locals(),who='美女'))
"""
>>
老毕喜欢年龄18的美女
老毕喜欢年龄19的美女
老毕喜欢年龄20的美女
老毕喜欢年龄21的美女
....
老毕喜欢年龄38的美女
"""
python版本: 3.4
替换字符串中的子串
"""
import string
info = """姓名: $name,
年龄: $age,
博客: $blog,
http://${weibo},
$$帅
"""
#string.Template(template)
info_template = string.Template(info)
#以字典的方式一一赋值
info_dic={'name':'毕小朋','age':30,'blog':'http://blog.csdn.net/wirelessqa','weibo':"www.weibo.com/wirelessqa"}
#substitute(mapping, **kwds)
print(info_template.substitute(info_dic))
"""
>>
姓名: 毕小朋,
年龄: 30,
博客: http://blog.csdn.net/wirelessqa, http://www.weibo.com,
$帅
"""
#转成字典后再赋值
info_dic2=dict(name='毕小朋',age=30,blog='http://blog.csdn.net/wirelessqa',weibo='www.weibo.com/wirelessqa')
print(info_template.substitute(info_dic2))
"""
>>
姓名: 毕小朋,
年龄: 30,
博客: http://blog.csdn.net/wirelessqa, http://www.weibo.com,
$帅
"""
#safe_substitute(mapping, **kwds)
#当我们少一个键(weibo被拿掉了哦)时,查看结果如何
test_safe_substitute=dict(name='毕小朋',age=30,blog='http://blog.csdn.net/wirelessqa')
try:
print(info_template.substitute(test_safe_substitute))
except KeyError:
print("error: 映射中没有weibo这个键")
"""
>>
error: 映射中没有weibo这个键
"""
#使用safe_substitute(mapping, **kwds)
print(info_template.safe_substitute(test_safe_substitute))
"""
>>
姓名: 毕小朋,
年龄: 30,
博客: http://blog.csdn.net/wirelessqa, http://${weibo},
$帅
"""
#locals()提供了基于字典的访问局部变量的方式
info = string.Template('老毕是$shuai,芳龄$age')
shuai='帅哥'
age=18
print(info.substitute(locals())) #>>老毕是帅哥,芳龄18
#使用关键字作为参数替换
info = string.Template('老毕喜欢年龄$age的$who')
for i in range(18,39):
print(info.substitute(age=i,who='美女'))
"""
>>
老毕喜欢年龄18的美女
老毕喜欢年龄19的美女
老毕喜欢年龄20的美女
老毕喜欢年龄21的美女
....
老毕喜欢年龄38的美女
"""
#同时使用关键字参数和字典
for age in range(18,39):
print(info.substitute(locals(),who='美女'))
"""
>>
老毕喜欢年龄18的美女
老毕喜欢年龄19的美女
老毕喜欢年龄20的美女
老毕喜欢年龄21的美女
....
老毕喜欢年龄38的美女
"""
相关文章推荐
- Python3.4字符串对齐/删除空格/合并/反转
- Python3.4字符串基础及每次处理一个字符
- Windows下Python添加库(模块)路径
- python检测是文件还是目录的方法
- python生成随机密码或随机字符串的方法
- 在Python 3中实现类型检查器的简单方法
- python urllib
- Python字符串的encode与decode研究心得乱码问题解决方法
- Python的getattr(),setattr(),delattr(),hasattr()
- codecademy字符串匹配——python
- 毕业纪念
- python 模块的安装
- 学习python
- python unicode 和 str 类型的关系
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python random
- python解析xml ElementTree
- Python学习之traceback模块
- head.First.python:注册pypi、上传代码、更新代码
- BoW(词袋模型)+python代码实现