from string import Template
s = '$who likes $what'
t = Template(s)
d = {'who': 'jianpx', 'what': 'mac'}
print t.substitute(d)
默认的分隔符是 $, 想要换掉这个分隔符的话,只需要继承Template类,然后更改其中的delimiter属性即可
#!/usr/bin/python
#coding :utf-8
from string import Template
class MyTemplate(Template):
"""docstring for MyTemplate"""
delimiter = '#'
def _test():
s = '#who likes #what'
t = MyTemplate(s)
d = {'who': 'jianpx', 'what': 'mac'}
print t.substitute(d)
print MyTemplate.delimiter
print Template.delimiter
if __name__ == '__main__':
_test()