Python:将一个变量保存为HTML文件
Python: Save a variable as HTML file
这可能是一个非常简单的问题,但直到现在我都没有找到任何解决方案,而且我一直在这几个小时
我有一个 HTML 文件“teste.html”
我使用 jinja 2 来改变我的 HTML
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("teste.html")
template_vars = {"Client" : Name}
html_out = template.render(template_vars)
type(html_out)
以 html_out 结束,即 s 字符串
type(html_out)
Out[60]: str
现在我想将此 html_out 保存在 HTML 文档中
我该怎么做?
您只需将 HTML 字符串写入扩展名为 .html
的文件即可。
例如:
htmlstring = ... # Your HTML data
with open("yourhtmlfile.html", "w") as file:
file.write(htmlstring)
听起来您想将 str
保存到文件中。在 python 中保存文件可以像这样完成:
with open('teste_rendered.html', 'w') as file:
file.write(html_out)
注意:文件实际保存的位置取决于您 运行 您的 python 脚本的方式。
这可能是一个非常简单的问题,但直到现在我都没有找到任何解决方案,而且我一直在这几个小时
我有一个 HTML 文件“teste.html” 我使用 jinja 2 来改变我的 HTML
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("teste.html")
template_vars = {"Client" : Name}
html_out = template.render(template_vars)
type(html_out)
以 html_out 结束,即 s 字符串
type(html_out)
Out[60]: str
现在我想将此 html_out 保存在 HTML 文档中
我该怎么做?
您只需将 HTML 字符串写入扩展名为 .html
的文件即可。
例如:
htmlstring = ... # Your HTML data
with open("yourhtmlfile.html", "w") as file:
file.write(htmlstring)
听起来您想将 str
保存到文件中。在 python 中保存文件可以像这样完成:
with open('teste_rendered.html', 'w') as file:
file.write(html_out)
注意:文件实际保存的位置取决于您 运行 您的 python 脚本的方式。