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. MySQL的Illegal mix of collationsy异常原因和解决方法

    原创 2008年12月25日 11:54:00 标签: mysql / collation / character / variables / database / server   今天在使用数据库 ...

  2. PhpStorm中如何使用Xdebug工具,入门级操作方法

    http://blog.csdn.net/knight_quan/article/details/51953269 1.简介: PhpStorm是一个轻量级且便捷的PHP IDE,其提供的智能代码补全 ...

  3. linux下yum命令出现Loaded plugins: fastestmirror

    yum install的时候提示:Loaded plugins: fastestmirror fastestmirror是yum的一个加速插件,这里是插件提示信息是插件不能用了. 不能用就先别用呗,禁 ...

  4. vue源码入口文件分析

    开发vue项目有段时间了, 之前用angularjs 后来用 reactjs 但是那时候一直没有时间把自己看源码的思考记录下来,现在我不想再浪费这 来之不易的思考, 我要坚持!! 看源码我个人感觉非常 ...

  5. 跟我一起读postgresql源码(十四)——Executor(查询执行模块之——Join节点(下))

    3.HashJoin 节点 postgres=# explain select a.*,b.* from test_dm a join test_dm2 b on a.xxx = b.xxx; QUE ...

  6. vue+springboot前后端分离实现单点登录跨域问题处理

    最近在做一个后台管理系统,前端是用时下火热的vue.js,后台是基于springboot的.因为后台系统没有登录功能,但是公司要求统一登录,登录认证统一使用.net项目组的认证系统.那就意味着做单点登 ...

  7. ORACLE SQL脚本能否修改字段名称?

    在看到标题时,你先想想:在ORACLE中能否修改一个表的某个字段名呢?如果能的话,你是否还记得SQL脚本如何写的呢,呵呵,写这个的目的是因为在论坛上看见许多信誓旦旦的说ORACLE中不能修改字段名称, ...

  8. struts 中的addFieldError

    addFieldError("字段名","错误信息")给一个字段(属性)添加错误消息 在action中添加 this.addFieldError("p ...

  9. win10汇编如何debug(小白向)

    先引用别人的方法 http://blog.csdn.net/lcr_happy/article/details/52491107 按他所说的下完对应软件后,安装其中的dosbox,然后将debug.e ...

  10. awk 字符串处理函数

    awk提供了许多强大的字符串函数,见下表:awk内置字符串函数gsub(r,s)    在整个$0中用s替代rgsub(r,s,t)    在整个t中用s替代rindex(s,t)    返回s中字符 ...