Python-String:Template
Template python string提供的一個字元串模板,要用於文本處理
from string import template
兩種substitute方法
1.substitute
# key必須被替代字元串配對,否則拋出ValueError
lan = Template('$python is $best')
lan.substitute(python = "C++",best ="also best")
'C++ is also best'
2.safe_substitute
# 不拋出任何異常,能配對的配對,不能的保留
lan.safe_substitute(python = "C++")
'C++ is $best'
lan.safe_substitute()
'$python is $best'
lan.safe_substitute(best="also best")
'$python is also best'
substitute(mapping ,**kwds)
lan = Template('$python is $best')
d = dict(python='time')
lan.safe_substitute(d)
'time is $best'
同時接受mapping and **kwd ,kwds優先
test_s = Template('$python is $best')
d=dict(python = 'favourite')
test_s.safe_substitute(d,python="aka")
'aka is $best'