string模板
string模块中包含了一个很有用的Template类,可以先写好字符串模板,后期使用的时候直接替换就可以了。
>>>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.'
>>> 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.'
>>> 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模板的更多相关文章
- Python - 定制pattern的string模板(template) 具体解释
定制pattern的string模板(template) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/28625179 ...
- Linux c++ 转string模板函数,写文件
#include <iostream> #include <sstream> using namespace std;//转string模板函数 template <cl ...
- ES6 Template String 模板字符串
模板字符串(Template String)是增强版的字符串,用反引号(`)标识,它可以当作普通字符串使用,也可以用来定义多行字符串,或者在字符串中嵌入变量. 大家可以先看下面一段代码: $(&quo ...
- 头文件 string.h cstring string 区别
1.#include <cstring> //不可以定义string s:可以用到strcpy等函数using namespace std; #include <stri ...
- Mustache.js前端模板引擎源码解读
mustache是一个很轻的前端模板引擎,因为之前接手的项目用了这个模板引擎,自己就也继续用了一会觉得还不错,最近项目相对没那么忙,于是就抽了点时间看了一下这个的源码.源码很少,也就只有六百多行,所以 ...
- STL 简介,标准模板库
这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL. 当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...
- C++ string::size_type 类型【转】
int main() { string str("Hello World!\n"); cout << "The size of " << ...
- Python3 学习第二弹: 字符串String
字符串表示问题 常见用法 '' 与 "" 就不提了 一些特殊用法 三引号:接收多行字符串的输入 >>>print('''Oh my God!''') Oh my ...
- Mvc Model 模板的获取【学习笔记】
MVC的Model模板有两种:一种编辑模式(@Html.EditorFor()).一种显示模式(Html.DisplayFor()). 模板的获取与执行(以下转自这里): 当我们调用HtmlHelpe ...
随机推荐
- java多线程向数据库写入数据
任务: 从sqlserver中将一个表A(约16W条数据)导到mysql中对应的一个表B中. 思路:分段获取A表中的数据后,用多个线程同时向B表中写入. 关键代码 //将数据库中的数据条数分段 pub ...
- 面向对象程序设计-C++_课时26拷贝构造Ⅰ_课时27拷贝构造Ⅱ
一旦写了一个类,给它3个函数: 1default construtor 2virtual destructor 3copy constructor Constructions vs. assignme ...
- uva 301 Transportation 铁路公司的阳谋 纯dfs暴力
题目比较难理解. 给出铁路的容量和站点数,以及几笔订单,要求算出如何盈利最大. 咋一看想贪心,但无法确定是最优解啊. 于是用dfs做,就两种状况,选与不选,先开一个每个站点的当前人数数组,假设要选,然 ...
- Ubuntu创建快捷方式
正常安装putty不成功:sudo apt-get install putty 总是报网络方面的错,我只好从官网下载源码自己编译.还是很容易的,找到unix目录,然后执行:make -f Makefi ...
- 利用CSS3的transform 3D制作的立方体旋转效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- JavaScript引用类型之Array数组之强大的splice()方法
splice()方法可以说是Array数组最强大的方法,他的用法很多,主要用法是向数组的中部插入项! 下面是它的用法: arrayObject.splice(index,howmany,element ...
- SQL中日期格式转换为年月日
)
- js生成验证码并且判断
<style type="text/css"> .code { font-family: Arial; ...
- 【原创】重绘winform的GroupBox
功能:重绘winform的GroupBox,以便调整边框颜色和边框宽度 using System; using System.Collections.Generic; using System.Com ...
- Mac OS X下Maven的安装与配置
Mac OS X 安装Maven: 下载 Maven, 并解压到某个目录.例如/Users/robbie/apache-maven-3.3.3 打开Terminal,输入以下命令,设置Maven cl ...