Python - 字符串模板的安全替换(safe_substitute) 具体解释
字符串模板的安全替换(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 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
转自: http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html 黄聪:Python 字符串操作(string替换.删除.截取. ...
- Python 字符串操作(截取/替换/查找/分割)
Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾. # 例1:字符串截取 str = '1234567 ...
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sS ...
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sS ...
- python 字符串模板
from string import Template print(type(Template)) mystr = Template("hi,$name 你是$baby") pri ...
- Python - 安全替换字符串模板(safe_substitute) 详细解释
安全替换字符串模板(safe_substitute) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27057339 字 ...
- python字符串截取与替换的例子
python字符串截取与替换的多种方法 时间:2016-03-12 20:08:14来源:网络 导读:python字符串截取与替换的多种方法,以冒号分隔的字符串的截取方法,python字符串替换方法, ...
- Python 字符串操作
Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
python3.4学习笔记(十五) 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) python print 不换行(在后面加上,end=''),prin ...
随机推荐
- B2460 [BeiJing2011]元素 线性基
这个题是对刚才线性基的一个补充,就是中间有一些小贪心,贪心就很有意思,先按权值排序,然后就瞎搞就行了. 题干: Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们 ...
- bzoj3673 & bzoj3674 & 洛谷P3402 可持久化并查集
题目:bzoj3673:https://www.lydsy.com/JudgeOnline/problem.php?id=3673 bzoj3674:https://www.lydsy.com/Jud ...
- RPC通信框架——RCF介绍
现有的软件中用了大量的COM接口,导致无法跨平台,当然由于与Windows结合的太紧密,还有很多无法跨平台的地方.那么为了实现跨平台,支持Linux系统,以及后续的分布式,首要任务是去除COM接口. ...
- pair的定义C++
template <class T1, class T2> struct std::pair { typedef T1 first_type; typedef T2 second_type ...
- 1tb等于多少g 1TB和500G有什么区别
转自:http://www.a207.com/article/view_39392 移动硬盘.U盘是生活中常见的用品,他们的内存大小是什么标准.很多人对于1tb等于多少g和1tb和500g有什么区别不 ...
- PCB MS SQL跨库执行SQL 获取返回值
一.SQL跨库执行SQL 获取返回值 ) DECLARE @sql nvarchar(MAX) DECLARE @layer INT SET @Dblink = 'P2.fp_db.dbo.' sel ...
- E20170813-ts
explicitly adv. 明白地,明确地;
- VS2015 右侧导航插件地址
右侧导航插件: https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.ProductivityPowe ...
- Php.ini文件位置在哪里 Php.ini文件找不到
转载自:http://www.php100.com/html/php/rumen/2013/0831/26.html [导读] Php ini文件是php的一个配置文件,在windows主机中如果你未 ...
- 【java基础】(1)Java的权限修饰符(public,protected,default,private)
访问权限修饰符权限从高到低排列是public ,protected ,default, private. 一.根据“是否是同包”.“是否是子类”分为4中情况+本类 5种情况 二.把 同包中的子类 ...