发布时间:2019-08-11 11:26:24编辑:auto阅读(2326)
本文地址: http://blog.csdn.net/caroline_wendy/article/details/28614491
string模板(template)参考: http://blog.csdn.net/caroline_wendy/article/details/27054263
string.Template()内添加替换的字符, 使用"$"符号, 或 在字符串内, 使用"${}"; 调用时使用string.substitute(dict)函数.
可以通过继承"string.Template", 覆盖变量delimiter(定界符)和idpattern(替换格式), 定制不同形式的模板.
代码:
# -*- coding: utf-8 -*-
'''
Created on 2014.6.5
@author: Administrator
@edition : python 3.3.0, eclipse pydev
'''
import string
template_text = '''
Delimiter : %%
Replaced : %with_underscore
Ingored : %notunderscored
'''
d = {'with_underscore' : 'replaced',
'notunderscored' : 'not replaced'}
class MyTemplate(string.Template):
delimiter = '%'
idpattern = '[a-z]+_[a-z]+'
t = MyTemplate(template_text)
print('Modified ID pattern: ')
print(t.safe_substitute(d))Modified ID pattern:
Delimiter : %
Replaced : replaced
Ingored : %notunderscored
上一篇: python-sendkeys 模拟键
下一篇: python线程、协程
54430
41194
35719
31274
26179
25895
24391
19930
15973
15419
2293°
2097°
2187°
2307°
2270°
2486°
2422°
2236°
2317°
2293°