from string import Template
template_string = '<html><head><title>$title</title></head><body>$body</body></html>'
t = Template(template_string)
t.substitute(title='hello', body='hello world')
好处是不用安装其他的第三方的包,缺点是比较简单,不能嵌入python代码
本文介绍了一种利用Python标准库中的fromstring模块来生成HTML的方法。这种方法无需额外安装第三方包,通过简单的字符串替换即可完成HTML文档的创建。但其功能相对有限,不支持在模板中嵌入复杂的Python代码。
from string import Template
template_string = '<html><head><title>$title</title></head><body>$body</body></html>'
t = Template(template_string)
t.substitute(title='hello', body='hello world')
好处是不用安装其他的第三方的包,缺点是比较简单,不能嵌入python代码
379
1956
4571

被折叠的 条评论
为什么被折叠?
