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. poj2871

    #include <stdio.h> #include <stdlib.h> //法一 int main() { ]; ,tmp; ) { scanf("%lf&qu ...

  2. Openstack 的 RPC使用。

    大家都已经很熟悉了RPC了. 比如说nfs就是采用的RPC通信. 尤其SUN RPC 已经成为了C语言开发的经典一种进程间调用机制. openstack 的RPC 机制, 是基于AMQP 活着其他高级 ...

  3. Linux系统CentOS6.2版本号下安装JDK7具体过程

    前言:        java 是一种能够撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE( ...

  4. linux下查看文件内容cat,more,less

    1. 查看文件内容经常使用的命令 cat : 由第一行显示文件内容 tac:  从最后一行開始显示.与cat相反 nl :  文件内容和行号一起输出 more: 一页一页显示 less: 与more类 ...

  5. oracle表设置主键自增长

    create or replace table TBL_SYS_USER (   user_id             NUMBER(19) not null,   user_name        ...

  6. 权威指南之脚本化http

    使用javascript代码操作http是可行的.当用脚本设置window对象的location属性或调用表单对象的submit方法时,都会初始化http请求.这两种情况下,浏览器会加载新页面. aj ...

  7. mywebsite1

    http://www.wuweierwei.com/ 个人网站 http://www.yyyweb.com/demo/icons-filling/index.html CSS3图标填充效果 http: ...

  8. JavaSE思维导图(二)

  9. SlidingMenu侧边菜单

    第一步.首先在你项目中创建一个包存放侧边菜单的类:

  10. Svn服务启动的两种方式

    一.svn服务器启动 › cmd命令行启动:vsvnserve -d –r 文档仓库路径 -d 后台执行 › -r 版本库的根目录 二.›Windows服务自动启动     利用xp.2000 以上的 ...