0
我有一个用作模板的xml文件。 我有这个XML中的多个标记将被实际数据替换。这是我做过什么:
通过标记字符串替换多个文本
def populate_template(self, value1, value2, value3):
with open('my_template.xml', 'rb') as xml_template:
template_string = xml_template.read()
template_string.replace('{{MARKER_1}}', value1)
template_string.replace('{{MARKER_2}}', value2)
template_string.replace('{{MARKER_3}}', value3)
return template_string
每个标记可以出现多次的模板中。
我想知道是否有更有效的方法来做到这一点?
高科技的东西:
- 的Python 2.7
谢谢。我改变了你的代码,使用'Environment#get_template'方法来获取模板,它的效果很好。谢谢! –