Python - 字符串模板的安全替换(safe_substitute) 具体解释
2017-07-02 12:03
429 查看
字符串模板的安全替换(safe_substitute) 具体解释
本文地址: http://blog.csdn.net/caroline_wendy/article/details/27057339字符串模板(sting.Template), 替换时, 使用substitute(), 未能提供模板所需的所有參数值时, 会发生异常.
假设使用safe_substitute(), 即安全替换, 则会替换存在的字典值, 保留未存在的替换符号.
代码:
# -*- coding: utf-8 -*- ''' Created on 2014.5.26 @author: C.L.Wang Eclipse Pydev python 2.7.5 ''' import string values = {'var' : 'foo'} t = string.Template('''$var is here but $ missing is not provided! ''') try: print 'substitute() : ', t.substitute(values) except ValueError as err: print 'Error:', str(err) print 'safe_substitude() : ', t.safe_substitute(values)
输出:
substitute() : Error: Invalid placeholder in string: line 1, col 18 safe_substitude() : foo is here but $ missing is not provided!
相关文章推荐
- Python - 安全替换字符串模板(safe_substitute) 详细解释
- Python - 字符串模板的安全替换(safe_substitute) 详解
- Python - 字符串的替换(interpolation) 具体解释
- Domino系统Notes数据库模板替换命令详细解释,包括具体的参数!!
- Python - 定制pattern的string模板(template) 具体解释
- Python字符串拼接、截取及替换方法总结
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python 字符串替换
- python 字符串替换
- Asp.net 字符串操作基类(安全,替换,分解等)
- Python正则表达式如何进行字符串替换
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转...
- Asp.net 字符串操作基类(安全,替换,分解等)
- 安全的字符串复制函数strcpy_safe的实现
- 将用户输入的字符串转换为可换行、替换Html编码、无危害数据库特殊字符、去掉首尾空白、的安全方便代码
- python字符串替换方法和注意事项
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python字符串替换的2种有效方法
- 通用安全字符串输入,彻底替换server.htmlencode
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)