python的Template
Template模块,可以用来制作web页面的模板,非常的方便。
Template属于string中的一个类,所以要使用的话要在头部引入:
from string import Template
模板替换变量采用的是$符号,而不是%,它的使用要遵循以下规则:
$$是需要规避,已经采用一个单独的$代替($$相当于输出$,而不是变量)$identifier变量由一个占位符替换(key),key去匹配变量"identifier"${identifier}相当于$identifier. 它被用于当占位符后直接跟随一个不属于占位符的字符,列如"${noun}ification"
Template中有两个重要的方法:substitute和safe_substitute,如下标红的方法名
class string.Template(template)
The constructor takes a single argument which is the template string. substitute(mapping, **kwds)
Performs the template substitution, returning a new string. mapping is any dictionary-like object with keys that match the placeholders in the template. Alternatively, you can provide keyword arguments, where the keywords are the placeholders. When both mapping and kwds are given and there are duplicates, the placeholders from kwds take precedence. safe_substitute(mapping, **kwds)
Like substitute(), except that if placeholders are missing from mapping and kwds, instead of raising a KeyError exception, the original placeholder will appear in the resulting string intact. Also, unlike with substitute(), any other appearances of the $ will simply return $ instead of raising ValueError. While other exceptions may still occur, this method is called “safe” because substitutions always tries to return a usable string instead of raising an exception. In another sense, safe_substitute() may be anything other than safe, since it will silently ignore malformed templates containing dangling delimiters, unmatched braces, or placeholders that are not valid Python identifiers.
测试代码--规则:
s1 = Template('$who likes $what')
print(s1.substitute(who='tim', what='kung pao'))
s2 = Template('${who}likes $what')
print(s2.substitute(who='tim', what='kung pao'))
s3 = Template('$$who likes $what')
print(s3.substitute(who='tim', what='kung pao'))
输出:
tim likes kung pao
timlikes kung pao
$who likes kung pao
测试代码--safe_substitute和substitute区别:
d = dict(who='java')
print(Template('$who need $100').safe_substitute(d))
print(Template('$who need $100').substitute(d))
输出:
java need $100
Traceback (most recent call last):
使用safe_substitute可以正常输出,而使用substitute会出错,需要把$100改成$$100
substitute比较严格,必须每一个占位符都找到对应的变量,不然就会报错,而safe_substitute则会把未找到的$XXX直接输出
参考资料:https://docs.python.org/3.4/library/string.html#template-strings
https://my.oschina.net/u/241670/blog/309856
python的Template的更多相关文章
- Python中Template使用的一个小技巧
Python中Template是string中的一个类,可以将字符串的格式固定下来,重复利用. from string import Template s = Template("there ...
- python的Template使用指南
本文主要讲解了python中Template使用方法以及使用技巧,非常实用,有需要的朋友可以参考下: Template无疑是一个好东西,可以将字符串的格式固定下来,重复利用.同时Template也可以 ...
- Python Jinja2 Template: YAML File Cisco Example Tutorial
原文链接:http://networkbit.ch/python-jinja-template/ template.txt如下: hostname {{ name }} interface Loopb ...
- python Template中substitute()的使用
在python中Template可以将字符串的格式固定下来,重复利用. Template属于string中的一个类,要使用他的话可以用以下方式调用: from string import Templa ...
- 使用Template格式化Python字符串
对Python字符串,除了比较老旧的%,以及用来替换掉%的format,及在python 3.6中加入的f这三种格式化方法以外,还有可以使用Template对象来进行格式化. from string ...
- python中常用的函数与库一
1, collections.deque 在python里如果我们用列表作为队列使用也是可以的,只是当从队尾删除或者增加元素的时候是很快的,但是从队首删除或者增加元素则要慢得多,这是因为在队首进行操作 ...
- python简单爬虫定时推送同花顺直播及荐股至邮箱
1.初衷:实践 2.技术:python requests Template 3.思路:根据直播页面获取评价最高的前十博主,定时爬行最新的消息和实战股票 4.思路:python 编辑简单邮件html模板 ...
- Python day 02
基础&运算符 今日概要 循环 字符串格式化 运算符 编码 内容回顾 & 补充 内容回顾 计算机基础 解释器python 2 和 python 3 语法 print input if / ...
- Testing shell commands from Python
如何测试shell命令?最近,我遇到了一些情况,我想运行shell命令进行测试,Python称为万能胶水语言,一些自动化测试都可以完成,目前手头的工作都是用python完成的.但是无法从Python中 ...
随机推荐
- IRequiresSessionState接口控制
刚刚接触.net web端的朋友都会被Session坑过,莫名其妙的不能读取Session数据,后来知道原来有IRequiresSessionState这个接口,不继承的就不能读取Session里面的 ...
- jdk 安装 环境变量配置
右键选择 计算机→属性→高级系统设置→高级→环境变量 1.系统变量→新建 变量名:JAVA_HOME 变量值:(变量值填写你的jdk的安装目录,例如本人是 C:\Program Files\Java\ ...
- ORACLE常用数值函数、转换函数、字符串函数
本文更多将会介绍三思在日常中经常会用到的,或者虽然很少用到,但是感觉挺有意思的一些函数.分二类介绍,分别是: 著名函数篇 -经常用到的函数 非著名函数篇-即虽然很少用到,但某些情况下却很实用 注:N表 ...
- 调用手机在线API获取手机号码归属地信息
手机在线(www.showji.com)始创于2001年,发展至今已拥有国内最准确.号段容量最大的手机号码归属地数据库系统, 目前号段容量将近33万条,每月保持两次以上规模数据更新,合作伙伴包括:百度 ...
- 2.0、Hibernate框架的简单搭建
一.Hibernate:是一个开放源代码的对象关系映射框架,对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句 ...
- 我的页面定制CSS代码(SimpleGamboge皮肤)
我的页面定制CSS代码,针对博客园SimpleGamboge皮肤. 调整: 1.左上图片更换为自己的头像 2.扩大左侧栏宽度,缩小右侧主栏宽度宽度 3.扩大内容页面的评论区宽度,工具图标靠左 4.去广 ...
- mysql 编译安装
mysql 编译安装方式: ```cd /home/oldboy/tools``` 创建 目录 if not have then mkd ...
- 无中间变量交换swap(a,b)
#include <stdio.h> /* 加减法 整型.浮点型(损失精度) */ void swap1(int *a,int *b) { *a=*a+*b; *b=*a-*b; *a=* ...
- MySQL 应用优化
一.使用连接池 二.减少对MySQL的访问 (A) 避免对同一数据做重复检索. (B) 使用查询缓存,MySQL的查询缓存会存储SELECT查询的命令文本和相应的结果. (C) 增加CACHE层 三. ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...