String. Template () adds the substituted character, uses the "$" symbol, or within the string, uses "${}"; Use the String.substitute (dict) function when calling.
Can be inherited by "string." Template ", covering variable delimiter (delimiters) and Idpattern (alternate format), customizing different forms of templates.
Code:
#-*-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 '}< C14/>class MyTemplate (String. Template):
delimiter = '% '
idpattern = ' [a-z]+_[a-z]+ '
t = MyTemplate (template_text)
print (' Modified ID pattern: ')
print (T.safe_substitute (d))
Output:
Modified ID pattern:
Delimiter:%
replaced:replaced
ingored:%notunderscored
Note: The delimiter (delimiter) is "%" and the replacement mode (Idpattern) must contain an underscore, so the 2nd is not replaced.
Author: csdn Blog spike_king
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/