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 ...
随机推荐
- linux 开通ftp账号
1.更新yum源 首先需要更新系统的yum源,便捷工具下载地址:http://help.aliyun.com/manual?spm=0.0.0.0.zJ3dBU&helpId=1692 2.安 ...
- find the mincost route(最小环,最短路,floyd)
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- Unity3d 游戏汉化之IL注入文本替换--木石世纪
近期下了个游戏叫木石世纪(Timber and Stone),沙盒游戏类,看着还不错. 搜了下游戏资料,有人求汉化可是因为是小众游戏,没人出汉化.看了眼是Unity3d的,既然是.Net的,仅仅要资源 ...
- web前端学习之路
test 随着自己对于web前端知识了解的越多,越来越发现自己真的好菜 一脸茫然阶段 两年前大学接触网页设计,那时对于网页设计一窍不通,只是看了一本自己大学编的一本入门教材,我甚至不知道那些网页设计的 ...
- Objective-C中的SEL (转载)
SEL 在Objective-C中,SEL是选择器(selector)的一个类型.选择器就是指向方法的一个指针,读者可以简单理解为程序运行到这里就会执行指定的方法,可以这样定义一个选择器: SEL ...
- Redis中各种方法的使用
①set ; i< ; i++) { // 不可以重复添加数据 client.AddItemToSet(KKey, "dong升-" + i); } client.Remov ...
- Notepad++使用技法
Alt+H 隐藏行 Ctrl+Tab 实现在多个打开的窗口间切换 Ctrl+Shift+Q区块注释 Ctrl+K行注释(取消Ctrl+Shift+K) 文件 新建文件 Ctrl+N 打开文件 C ...
- codeforces 632C. The Smallest String Concatenation 排序
题目链接 给出n个字符串, 将他们连在一起, 求连玩之后字典序最小的那种情况. 按a+b<b+a排序.... #include <iostream> #include <vec ...
- composer api 参考
composer note 简介 composer 是php的依赖管理工具.可以声明项目所依赖的库,composer会帮我们安装上 composer 默认基于项目来管理和安装库(包),将依赖的库安装到 ...
- 安装AppManager
http://www.searchvirtual.com.cn/showcontent_66884.htm