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使用的一个小技巧的更多相关文章

  1. 【每日一个小技巧】Python | input的提示信息换行输出,提示信息用变量表示

    [每日一个小技巧]Python | input的提示信息换行输出,提示信息用变量表示 在书写代码的途中,经常会实现这样功能: 请输入下列选项前的序号: 1.选择1 2.选择2 3.选择3 在pytho ...

  2. Django中的ORM框架使用小技巧

      Django中的ORM框架使用小技巧 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. Django对各个数据提供了很好的支持,包括PostgreSQL,MySQL,SQLite ...

  3. Python对list操作的一些小技巧

    Python对list操作的一些小技巧  由于要搞数学建模,于是从熟悉已久的C++转战Python.虽然才上手,但是Python的语法糖就让我大呼过瘾.不得不说相比于C/C++,Python对于数据的 ...

  4. 快速掌握iOS API的一个小技巧

    快速掌握iOS API的一个小技巧 周银辉 iOS SDK和Developer Library中提供了各个类以及函数的帮助文档,这很棒,但要想了解整个库的大体结构(比如UIKit下有哪些类,他们的继承 ...

  5. 【flash】关于flash的制作透明gif的一个小技巧

    关于flash的制作透明gif的一个小技巧 或者说是一个需要注意的地方 1.导出影片|gif,得到的肯定是不透明的.2.想要透明背景,必须通过发布.3.flash中想要发布gif动画的话,不能有文字, ...

  6. Python 练习冊,每天一个小程序

    Python 练习冊,每天一个小程序 说明:     Github 原文地址: 点击打开链接 Python 练习冊.每天一个小程序.注:将 Python 换成其它语言,大多数题目也试用 不会出现诸如「 ...

  7. 【前端】javascript中10常用的个小技巧总结

    javascript中10常用的个小技巧总结 本文转自:http://www.cnblogs.com/libin-1/p/6756393.html 1. new Set() 可能有人知道ES6中提供了 ...

  8. linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol &#39;pthread_create@@GLIBC_2.2.5&#39;

    解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件里引入了#include "cocos-ext.h",在进行C++编译的时候会遇到例如 ...

  9. http://www.yyne.com/python使用-urllib-quote-进行-url-编码小技巧/

    http://www.yyne.com/python使用-urllib-quote-进行-url-编码小技巧/

随机推荐

  1. radiobutton独特属性

    radiobutton是通过name来分组的,也就是说,使用相同的名字的radio,它们才是单选的,如果名字不同的radio,是不具备这个效果的,这个是第一要点. 第二,针对不同的radio(name ...

  2. Use LiveCD to acquire images from a VM

    Forensic examiners usually acquire images from suspect's PC or Laptop. What if the target computer i ...

  3. 如何给网站添加CNZZ站长统计功能代码的常用办法

    前几天有个客户来问小编怎么给网站添加上CNZZ站长统计工具,其实这个很简单,只要把cnzz免费代码复制到我们的footer文件就行.今天小编正好有空就来分享一下具体的操作过程. 首先要想获得这个免费的 ...

  4. css中,如何设置前景色的透明度?

    谢谢 我等待的他 | 浏览 255446 次 推荐于2016-01-13 12:27:03 最佳答案 css控制透明度倒不麻烦. filter:alpha(opacity=50); -moz-opac ...

  5. dede内容页调用图片集下所有图片方法!

    http://blog.csdn.net/forest_fire/article/details/50943765 版权声明:本文为博主原创文章,未经博主允许不得转载. {dede:productim ...

  6. tp5 隐藏index.php 邓士鹏

    tp5 隐藏index.php ------------------------------------------------------------------------------------ ...

  7. ThinkPHP5上传图片并压缩为缩略图

    使用thinkphp开发app后端中,需要实现一个处理上传图片队列的功能 这是个上传多图片保存并且需要对其中一张图片进行压缩的功能 (使用的html5 mui框架开发app,如果直接载入原图,app客 ...

  8. Performance Testing 入门小结

    从事软件测试两年多了,一直在做功能测试.2016年计划学习Performance.今天,先把之前听过的同事session以及自己查阅的资料小结一下. 一.什么是性能测试 首先来说一下软件的性能是什么. ...

  9. ANSI C与C89、C99、C11区别差异

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  10. junit源码解析--核心类

    JUnit 的概念及用途 JUnit 是由 Erich Gamma 和 Kent Beck 编写的一个开源的单元测试框架.它属于白盒测试,只要将待测类继承 TestCase 类,就可以利用 JUnit ...