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. java多线程向数据库写入数据

    任务: 从sqlserver中将一个表A(约16W条数据)导到mysql中对应的一个表B中. 思路:分段获取A表中的数据后,用多个线程同时向B表中写入. 关键代码 //将数据库中的数据条数分段 pub ...

  2. 面向对象程序设计-C++_课时26拷贝构造Ⅰ_课时27拷贝构造Ⅱ

    一旦写了一个类,给它3个函数: 1default construtor 2virtual destructor 3copy constructor Constructions vs. assignme ...

  3. uva 301 Transportation 铁路公司的阳谋 纯dfs暴力

    题目比较难理解. 给出铁路的容量和站点数,以及几笔订单,要求算出如何盈利最大. 咋一看想贪心,但无法确定是最优解啊. 于是用dfs做,就两种状况,选与不选,先开一个每个站点的当前人数数组,假设要选,然 ...

  4. Ubuntu创建快捷方式

    正常安装putty不成功:sudo apt-get install putty 总是报网络方面的错,我只好从官网下载源码自己编译.还是很容易的,找到unix目录,然后执行:make -f Makefi ...

  5. 利用CSS3的transform 3D制作的立方体旋转效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. JavaScript引用类型之Array数组之强大的splice()方法

    splice()方法可以说是Array数组最强大的方法,他的用法很多,主要用法是向数组的中部插入项! 下面是它的用法: arrayObject.splice(index,howmany,element ...

  7. SQL中日期格式转换为年月日

    )

  8. js生成验证码并且判断

       <style type="text/css">        .code        {            font-family: Arial;      ...

  9. 【原创】重绘winform的GroupBox

    功能:重绘winform的GroupBox,以便调整边框颜色和边框宽度 using System; using System.Collections.Generic; using System.Com ...

  10. Mac OS X下Maven的安装与配置

    Mac OS X 安装Maven: 下载 Maven, 并解压到某个目录.例如/Users/robbie/apache-maven-3.3.3 打开Terminal,输入以下命令,设置Maven cl ...