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 ...
随机推荐
- Arranging Your Team
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=35800#problem/D #include <iostream> #inc ...
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- Java中的常用类有哪些
1NumberFormat 2DecimalFormat 3BigDecimal 4Math 5Random 6DateFormat 7SimpleDateFormat 8Calendar 9Date ...
- pgsql 远程机器无法连接数据库报错处理方法
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本地localhost访问正常,在相同网段的远程机器访问报如下错误 “server closed the connection unexpe ...
- Spring Boot (16) logback和access日志
Spring Boot 内部采用的是Commons Logging进行日志记录,但是在底层为Java Util Logging.Log4J2.Logback等日志框架提供了默认配置. logback ...
- TypeError: Object function (req, res, next) { app.handle(req, res, next); } has no method 'configure'
TypeError: Object function (req, res, next) { app.handle(req, res, next); } has no method 'configure ...
- .htaccess的基本用法与介绍
●自定义错误页 .htaccess的一个应用是自定义错误页面,这将使你可以拥有自己的.个性化的错误页面(例如找不到文件时),而不是你的服务商提供的错误页或没有任何页面.这会让你的网站在出错的时候看上去 ...
- 【sqli-labs】 less25a GET- Blind based -All you OR&AND belong to us -Intiger based(GET型基于盲注的去除了or和and的整型注入)
因为过滤是针对输入的字符串进行的过滤,所以如果过滤了or and的话,提交id=1和id=and1结果应该相同 http://localhost/sqli-labs-master/Less-25a/? ...
- c# 验证码实现代码
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D ...
- Java基础学习笔记三 正则表达式和校验、Date、DateFormat、Calendar
正则表达式 正则表达式(英语:Regular Expression,在代码中常简写为regex).正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串.在 ...