看Python帮助文档模板部分,抄下来以备记忆

11.2. Templating

The string module includes a versatile Template class with a simplified syntax suitable for editing by end-users. This allows users to customize their applications without having to alter the application.

The format uses placeholder names formed by $ with valid Python identifiers (alphanumeric characters and underscores). Surrounding the placeholder with braces allows it to be followed by more alphanumeric letters with no intervening spaces. Writing $$ creates a single escaped $:

>>>from string import Template >>>t = Template('${village}folk send $$10 to $cause.') >>>t.substitute(village='Nottingham', cause='the ditch fund') 'Nottinghamfolk send $10 to the ditch fund.'

The substitute() method raises a KeyError when a placeholder is not supplied in a dictionary or a keyword argument. For mail-merge style applications, user supplied data may be incomplete and the safe_substitute() method may be more appropriate — it will leave placeholders unchanged if data is missing:

>>> 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 subclasses can specify a custom delimiter. For example, a batch renaming utility for a photo browser may elect to use percent signs for placeholders such as the current date, image sequence number, or file format:

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

Another application for templating is separating program logic from the details of multiple output formats. This makes it possible to substitute custom templates for XML files, plain text reports, and HTML web reports.

python 论坛模板_python模板 - PH的个人空间 - OSCHINA - 中文开源技术交流社区相关推荐

  1. python颜色相关系数_python相关系数 - osc_w6qmyr6s的个人空间 - OSCHINA - 中文开源技术交流社区...

    皮尔逊相关系数: 用于度量两个变量X和Y之间的相关(线性相关),其值介于-1与1之间. 几组 的点集,以及各个点集中 和 之间的相关系数.我们可以发现相关系数反映的是变量之间的线性关系和相关性的方向( ...

  2. python 状态机第三方库_Python 状态机 - osc_8g11urw7的个人空间 - OSCHINA - 中文开源技术交流社区...

    class StateMachine: def __init__(self): self.handlers = {} # 状态转移函数字典 self.startState = None # 初始状态 ...

  3. python病毒代码大全_python病毒 - osc_bx0x099p的个人空间 - OSCHINA - 中文开源技术交流社区...

    介绍 源码 分为3个部分 1.搜索,搜寻所有的python脚本 2.取出当前文件的前39行,也就是这个脚本的长度,然后将这个脚本写道所有找到的python脚本中去 3.其他行为 #!/usr/bin/ ...

  4. python sqlite3加密_sqlite3加密 - kjpioo的个人空间 - OSCHINA - 中文开源技术交流社区...

    SQLite 3 开源版不带加密功能,对于一个保存在本地的数据库来说没有加密功能让人难以接受,只要用记事本打开数据库就可以看到数据库内保存的数据,对安全多多少少有一点影响.有一个办法是把内容加密后保存 ...

  5. python莫比乌斯_莫比乌斯函数 - osc_7eqzxl4g的个人空间 - OSCHINA - 中文开源技术交流社区...

    前导 要学习莫比乌斯函数 需要学习 到 积性函数,深度理解欧拉筛. 先说说什么是积性函数吧. 积性函数 其实积性函数非常好理解, 定义 积性函数:若gcd(a,b)=1,且满足f(ab)=f(a)f( ...

  6. python列表去重效率_python面试题 - osc_yztbpii7的个人空间 - OSCHINA - 中文开源技术交流社区...

    1.一行代码实现1--100之和 In [1]: sum(range(1,101)) Out[1]: 5050 1-100求和 2.如何在一个函数内部修改全局变量 a=520 deffun():glo ...

  7. python 条件表达式换行_Python基础语法 - LongKing-Xu的个人空间 - OSCHINA - 中文开源技术交流社区...

    python基础语法 一.标识符 在Python中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. 在Python中的标识符是区分大小写的. 在Python中以下划线开头的标识符是有 ...

  8. python变量无需指定类型对吗_Python变量类型 - osc_3rgq3dae的个人空间 - OSCHINA - 中文开源技术交流社区...

    变量存储在内存中的值,这就意味着在创建变量时会在内存开辟一个空间. 基于变量的数据类型,解析器会分配指定内存,并决定什么数据可以被存储在内存中. 因此变量可以指定不同的数据类型,这些变量可以存储整数. ...

  9. python蓝牙上位机开发_python做上位机 - osc_2frv0wjp的个人空间 - OSCHINA - 中文开源技术交流社区...

    参考文章: https://blog.csdn.net/dgut_guangdian/article/details/78391270 https://www.cnblogs.com/lanceyu/ ...

最新文章

  1. 根据ip获取地理位置
  2. 小程序 sha1和服务器有关系吗,微信小程序使用sha1实现密码加密的方法介绍
  3. 漫画:毕昇 JDK,重现了 “活字印刷术” 的传奇
  4. 在linux上配置JDK环境变量
  5. java 网站源码 四套模版 兼容手机平板PC 在线编辑模版 freemaker 静态引擎
  6. 看Quick Audience 如何有效提升营销活动管理效率
  7. [Jarvis OJ - PWN]——[XMAN]level3_x64
  8. C++ Priemer目录索引
  9. 游戏窗口组合键消息失败_5失败的投资组合,以后我在这里
  10. simulink学习笔记(2)
  11. 疫情当前,企业“逆势而上”还需“上云”加速
  12. Visual C# 资源文件编程--使用资源文件
  13. Memcached实战之单机部署----单实例/多实例
  14. CentOS7.1安装 Vsftpd FTP 服务器
  15. openssl代码领读目录
  16. BitPlots包简介
  17. 腾讯员工举报漏洞被逮捕,“白帽子”的行为边界到底在哪儿?
  18. 相控阵基础之3-阵列码间串扰
  19. 免费隐私保护国外域名注册商namecheap教程
  20. 个人GitHub地址

热门文章

  1. Spark _04集群搭建及测试
  2. extract local variale 和 jsp中查找选中内容的快捷键
  3. java基础---多线程之交替打印,等待唤醒机制
  4. 【Docker】Docker操作常用命令
  5. netty系列之:netty初探
  6. python根据表格数据生成折线图_Python交互图表可视化Bokeh:4. 折线图| 面积图
  7. 聊聊Java中的并发队列中 有界队列和无界队列的区别
  8. 深入浅出理解 Variable used in lambda expression should be final or effectively final
  9. 【易懂】Java源码角度分析put()与putIfAbsent()的区别——源码分析系列
  10. LinkedBlockingQueue的put,take方法