string模块中包含了一个很有用的Template类,可以先写好字符串模板,后期使用的时候直接替换就可以了。

        模板中使用$作为占位符前缀,使用{}包裹占位符以支持间断的标量名,使用$$转义$。例如:
  1.  >>>from string importTemplate
    >>> t =Template('${village}folk send $$10 to $cause.')
    >>> t.substitute(village='Nottingham', cause='the ditch fund')
    'Nottinghamfolk send $10 to the ditch fund.'
        如果字典中没有提供相应的替换参数,substitute()将抛出KeyError异常。
        如果只想替换部分参数,可以使用safe_substitute()方法。例如
  1. >>> t =Template('Return the $item to $owner.')
    >>> d = dict(item='unladen swallow')
    >>> t.substitute(d)
    Traceback(most recent call last):
    ...
    KeyError:'owner'
    >>> t.safe_substitute(d)
    'Return the unladen swallow to $owner.'
        Template的子类中可以自定义分隔符,例如,下面的批量重命名使用%作为分隔符
  1.  >>> import time, os.path
    >>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
    >>> class BatchRename(Template):
    ... delimiter = '%'
    >>> fmt = raw_input('Enter rename style (%d-date %n-seqnum %f-format): ')
    Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f >>> t = BatchRename(fmt)
    >>> date = time.strftime('%d%b%y')
    >>> for i, filename in enumerate(photofiles):
    ... base, ext = os.path.splitext(filename)
    ... newname = t.substitute(d=date, n=i, f=ext)
    ... print '{0} --> {1}'.format(filename, newname) img_1074.jpg --> Ashley_0.jpg
    img_1076.jpg --> Ashley_1.jpg
    img_1077.jpg --> Ashley_2.jpg

string模板的更多相关文章

  1. Python - 定制pattern的string模板(template) 具体解释

    定制pattern的string模板(template) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/28625179 ...

  2. Linux c++ 转string模板函数,写文件

    #include <iostream> #include <sstream> using namespace std;//转string模板函数 template <cl ...

  3. ES6 Template String 模板字符串

    模板字符串(Template String)是增强版的字符串,用反引号(`)标识,它可以当作普通字符串使用,也可以用来定义多行字符串,或者在字符串中嵌入变量. 大家可以先看下面一段代码: $(&quo ...

  4. 头文件 string.h cstring string 区别

    1.#include <cstring>   //不可以定义string s:可以用到strcpy等函数using   namespace   std; #include <stri ...

  5. Mustache.js前端模板引擎源码解读

    mustache是一个很轻的前端模板引擎,因为之前接手的项目用了这个模板引擎,自己就也继续用了一会觉得还不错,最近项目相对没那么忙,于是就抽了点时间看了一下这个的源码.源码很少,也就只有六百多行,所以 ...

  6. STL 简介,标准模板库

    这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL.  当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...

  7. C++ string::size_type 类型【转】

    int main() { string str("Hello World!\n"); cout << "The size of " << ...

  8. Python3 学习第二弹: 字符串String

    字符串表示问题 常见用法 '' 与 "" 就不提了 一些特殊用法 三引号:接收多行字符串的输入 >>>print('''Oh my God!''') Oh my ...

  9. Mvc Model 模板的获取【学习笔记】

    MVC的Model模板有两种:一种编辑模式(@Html.EditorFor()).一种显示模式(Html.DisplayFor()). 模板的获取与执行(以下转自这里): 当我们调用HtmlHelpe ...

随机推荐

  1. Gdal 1.11.0 添加 Postgresql 9.1 sqlite3 支持

    OS环境Ubuntu12.04 32bit 因为公司一个功能要用到gdal 的ogr2ogr命令转换shp数据,需要能往postgis和sqlite 中插入数据. 用gdal1.11.0的源码默认安装 ...

  2. oracle导入导出exp,imp

    exp dadifilm/oracle@dg file=/tmp/dadi.dmp full=y imp u_data/321@dg1  file=/dadi_desc.dmp Import: Rel ...

  3. drupal7中CKEditor开启上传图片功能

    在drupal建站中,所见即所得编辑器提供了友好的界面.也提高开发效率,而CKEditor是一款非常不错的编辑器.定制性相当高,在这推荐给大家. CKEditor和其它模块(IMCE)搭配下在文字排版 ...

  4. USB OTG简单介绍

    1 引言 随着USB2.0版本号的公布,USB越来越流行,已经成为一种标准接口.如今,USB支持三种传输速率:低速(1.5Mb/s).全速(12Mb/s)和快速(480Mb/s),四种传输类型:块传输 ...

  5. mysql关联更新

    update tb_sdd_info a,tb_bnm_evian_info b set a.username=b.username where a.username=b.memberno and  ...

  6. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  7. Spoken English

    The Business lunch 9.商务午餐怎样开场?[0731] Is this your first time in shanghai? 10.怎样询问对方的感受?[0801] How do ...

  8. SQL Server索引进阶:第十二级,创建,修改,删除

    在第十级中我们看到了索引的内部结构,在第十一级中我们看到了平衡树结构潜在的负面影响:索引碎片.有了索引内部结构的知识,我们可以检查在执行数据定义语句和数据操作语句的时候,都发生了什么.在本级中我们介绍 ...

  9. android UI-EditText的长度监听慎用TextWatcher

    在用户昵称的输入时,限定8个字符,本意是在输入超过8个时候,页面toast一个提示,就是下面的TextWatcher的监听,在afterTextChanged中处理. 原bug:huawei MT2- ...

  10. visio 2013 破解工具 - KMSpico

    背景:环境是 win7, 64 bit装了 visio 2013 , 可以却不能用它来画图,在网上找了一些破解工具,大都不能解决问题.网上不靠谱的广告型文章太多了,比较头痛. 所幸,终于找到正确的破解 ...