摘自:python参考手册.

string模块定义了一种新字符串类型Template,简化了特定的字符串置换操作,

Template定义一个类

1.template(s),  #s是字符串

s='hello,$world!'   #template 的置换符属性delimiter 默认是$,

2.t=template(s) #定义template的对象

template的对象t支持的方法

3.t.substitute(m,[,**kwargs])  #其中m是一个字典(或一个关键字参数列表),会对字符串s进行关键字置换,

t.substitute({'world':'nimei'})

再打印s。发现world被替换成nimei

>>> from string import Template
>>> s='hello,$world!'
>>> t=Template(s)
>>> t.substitute({'world':'nimei'})
'hello,nimei!'
>>>
转载!
#!/usr/bin/python
#coding :utf-8
from string import Template
class MyTemplate(Template):
"""docstring for MyTemplate"""
delimiter = '#' def _test():
s = '#who likes #what'
t = MyTemplate(s)
d = {'who': 'jianpx', 'what': 'mac'}
print t.substitute(d)
print MyTemplate.delimiter
print Template.delimiter
if __name__ == '__main__':
_test() 打印结果:
>>>
jianpx likes mac
#
$

浅析python的string.Template的更多相关文章

  1. python中string模块各属性以及函数的用法

    任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作.     python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串 ...

  2. 【转】浅析Python中的struct模块

    [转]浅析Python中的struct模块 最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概 ...

  3. python的string用法

    s.strip().lstrip().rstrip(',') S.lower() #小写 S.upper() #大写 S.swapcase() #大小写互换 S.capitalize() #首字母大写 ...

  4. Python 常用string函数

    Python 常用string函数 字符串中字符大小写的变换 1. str.lower()   //小写>>> 'SkatE'.lower()'skate' 2. str.upper ...

  5. 浅析python日志重复输出问题

    浅析python日志重复输出问题 问题起源: ​ 在学习了python的函数式编程后,又接触到了logging这样一个强大的日志模块.为了减少重复代码,应该不少同学和我一样便迫不及待的写了一个自己的日 ...

  6. 关于string.Template的简单介绍

    一.简介 string模块定义了一种新字符串类型Template,它简化了特定的字符串置换操作. 何谓“简化”?我们可以先想一下我们之前比较常用的有关字符串的“置换”操作有哪些:一种是利用%操作符实现 ...

  7. python中string格式化

    python中可以对string, int, float等数据类型进行格式化操作.下面举例来说明一些常用操作. 先贴出 python 对 String Formatting Operations 讲解 ...

  8. PyQt的QString和python的string的区别

    转载于http://blog.chinaunix.net/uid-200142-id-4018863.html python的string和PyQt的QString的区别 python string和 ...

  9. ES6, Angular,React和ABAP中的String Template(字符串模板)

    String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专 ...

随机推荐

  1. 项目积累——CSS应用

    <tr onmouseover=" this.style.backgroundColor= '#E0FFFF' "  onmouseout="this.style. ...

  2. OC基础(16)

    autorelease基本使用 autorelease注意事项 *:first-child { margin-top: 0 !important; } body > *:last-child { ...

  3. 在word中做复选框打对勾钩

    在word中做复选框打对勾钩 现在终于搞明白正确的操作方法 一.你在word里输入2610,按alt+X就能出 空checkbox 你在word里输入2611,按alt+X就能出 打了勾的checkb ...

  4. 树状数组POJ2352星星

    http://poj.org/problem?id=2352 这道题的题意对于住学者应该比较难理解,但是如果弄明白他的意思的话,你就会发现这就是赤裸裸的树状数组,哎,欺负我不懂是吧,当时读题读啦好久, ...

  5. Android开发中,那些让你相见恨晚的方法、类或接口

    1.getParent().requestDisallowInterceptTouchEvent(true);剥夺父view 对touch 事件的处理权,谁用谁知道. 2.ArgbEvaluator. ...

  6. 请慢慢移动……由于操作快慢导致的bug

    最近的工作中,遇到一个由于操作快慢不同导致的bug,原因是,操作慢的时候程序接收到了停止操作,继续处理正确,而快速操作的时候程序来不及处理操作停止的动作,导致需要传入的数据值已经改变,程序报错.这种缺 ...

  7. [UI]Flat UI - Free Boorstrap Framework and Theme

    ---------------------------------------------------------------------------------------------------- ...

  8. 【翻译习作】 Windows Workflow Foundation程序开发

    近期整理硬盘,把09年的翻译习作<Windows Workflow Foundation程序开发>找出来了.现在又把译文过了一遍,做了些修改,贴出来献丑了.原书是<Programmi ...

  9. windows内核对象可以等待

    内核对象有两种状态 触发 与未触发. 是可以等待的.

  10. Linq把一个DataTable根据一列去除重复数据

    DataTable dt_temp = dt.AsEnumerable().Cast<DataRow>().GroupBy(p => p.Field<string>(&q ...