Python中Template使用的一个小技巧
Python中Template是string中的一个类,可以将字符串的格式固定下来,重复利用。
from string import Template
s = Template("there are ${howmany} ${lang} Quotation symbols")
print s.substitute(lang='Python',howmany=3)
>>>there are 3 Python Quotation symbols
用法很简单,先生成一个模板实例s,然后调用替换函数substitute()将模板中的两个地方替换掉。替换的内容是通过字典对调用的,所以下面(lang='Python',howmany=3)出现的顺序可以不用严格的和模板中的一样。当然不用括号也是可以的。
from string import Template
s = Template("there are $howmany $lang Quotation symbols")
print s.substitute(lang='Python',howmany=3)
>>>there are 3 Python Quotation symbols
注意:在使用${howmany} ${lang}时候,括号里的内容和括号要紧贴着,不然会报错。
from string import Template
s = Template("there are ${ howmany } ${lang} Quotation symbols")
print s.substitute(lang='Python',howmany=3)
>>>Traceback (most recent call last):
File "E:/�������/201703/DeepLearning/neural-networks-and-deep-learning-master/src/validation.py", line 39, in <module>
print s.substitute(lang='Python',howmany=3)
File "C:\Users\wangxin\Anaconda2\lib\string.py", line 176, in substitute
return self.pattern.sub(convert, self.template)
File "C:\Users\wangxin\Anaconda2\lib\string.py", line 173, in convert
self._invalid(mo)
File "C:\Users\wangxin\Anaconda2\lib\string.py", line 146, in _invalid
(lineno, colno))
ValueError: Invalid placeholder in string: line 1, col 11
当然在使用substitute()的时候,对应的关键字和值都要给出,不然会报错。
from string import Template
s = Template("there are ${ howmany } ${lang} Quotation symbols")
print s.substitute(lang='Python')
>>>Traceback (most recent call last):
File "E:/�������/201703/DeepLearning/neural-networks-and-deep-learning-master/src/validation.py", line 39, in <module>
print s.substitute(lang='Python')
File "C:\Users\wangxin\Anaconda2\lib\string.py", line 176, in substitute
return self.pattern.sub(convert, self.template)
File "C:\Users\wangxin\Anaconda2\lib\string.py", line 166, in convert
val = mapping[named]
KeyError: 'howmany'
使用safe_substitute()可以避免报错.
from string import Template
s = Template("there are ${howmany} ${lang} Quotation symbols")
print s.safe_substitute(lang='Python')
>>>there are ${howmany} Python Quotation symbols
Python中Template使用的一个小技巧的更多相关文章
- 【每日一个小技巧】Python | input的提示信息换行输出,提示信息用变量表示
[每日一个小技巧]Python | input的提示信息换行输出,提示信息用变量表示 在书写代码的途中,经常会实现这样功能: 请输入下列选项前的序号: 1.选择1 2.选择2 3.选择3 在pytho ...
- Django中的ORM框架使用小技巧
Django中的ORM框架使用小技巧 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Django对各个数据提供了很好的支持,包括PostgreSQL,MySQL,SQLite ...
- Python对list操作的一些小技巧
Python对list操作的一些小技巧 由于要搞数学建模,于是从熟悉已久的C++转战Python.虽然才上手,但是Python的语法糖就让我大呼过瘾.不得不说相比于C/C++,Python对于数据的 ...
- 快速掌握iOS API的一个小技巧
快速掌握iOS API的一个小技巧 周银辉 iOS SDK和Developer Library中提供了各个类以及函数的帮助文档,这很棒,但要想了解整个库的大体结构(比如UIKit下有哪些类,他们的继承 ...
- 【flash】关于flash的制作透明gif的一个小技巧
关于flash的制作透明gif的一个小技巧 或者说是一个需要注意的地方 1.导出影片|gif,得到的肯定是不透明的.2.想要透明背景,必须通过发布.3.flash中想要发布gif动画的话,不能有文字, ...
- Python 练习冊,每天一个小程序
Python 练习冊,每天一个小程序 说明: Github 原文地址: 点击打开链接 Python 练习冊.每天一个小程序.注:将 Python 换成其它语言,大多数题目也试用 不会出现诸如「 ...
- 【前端】javascript中10常用的个小技巧总结
javascript中10常用的个小技巧总结 本文转自:http://www.cnblogs.com/libin-1/p/6756393.html 1. new Set() 可能有人知道ES6中提供了 ...
- linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件里引入了#include "cocos-ext.h",在进行C++编译的时候会遇到例如 ...
- http://www.yyne.com/python使用-urllib-quote-进行-url-编码小技巧/
http://www.yyne.com/python使用-urllib-quote-进行-url-编码小技巧/
随机推荐
- vue -- v-cloak解决刷新或者加载出现闪烁(显示变量)
在使用vue绑定数据的时候,渲染页面时会出现变量闪烁,例如 <div class="#app"> <p>{{value.name}}</p> & ...
- java小入门的感觉
工作两三年,.NET与Java都干过,也都是应付差事,用着现有的框架,现有的规范,实现简单的功能,有余力的情况下,看看框架中的代码,欣赏一下前辈们的心血,居然在单位也算有心的了?! 最近的JAVA项目 ...
- for循环去重排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Lucene学习笔记2-Lucene的CRUD(V7.1)
在进行CRUD的时候请注意IndexWriterConfig的设置. public class IndexCRUD { "}; private String citys[]={"j ...
- dede列表标签list:应用大全 {dede:list}
http://syizq.blog.163.com/blog/static/435700372011616115826329/ 标签名称: list 功能说明: 表示列表模板里的分页内容列表 适用范围 ...
- label+input实现开关切换效果
Document 主要使用label+input来实现改变left的值,下面是核心代码,意思就是选中的input的兄弟节点.box下的.switch-btn元素的left会变成0px(原来是-37px ...
- Number()和new Number()的区别以及一种简单实现
看MDN Beginners文档的时候注意到了这种用法 var n1 = Number(123); , 冒出的第一个疑问就是和 var n2 = new Number(123); 有什么区别呢? 首先 ...
- eclipse 用maven创建web项目
Eclipse 用maven构建web项目 (2013-01-27 11:05:31) 转载▼ 标签: it eclipse maven spring web 杂谈 一.背景介绍 对于初学者,用m ...
- MySQL 水平拆分(读书笔记整理)
转:http://blog.csdn.net/mchdba/article/details/46278687 1,水平拆分的介绍 一般来说,简单的水平切分主要是将某个访问极其平凡的表再按照某个字段的某 ...
- Xpath语法学习
贴几个我学习Xpath的参考 1 基本使用的参考 XPath学习:基本语法(一) 2 较为详细且清晰例子参考,推荐 XPath 详解,总结 3 详细语法参考 Xpath语法格式整理 4 官方参考 XP ...