gpt4 book ai didi

python - 我如何使用 jinja2 逐行将文件呈现为 html

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:01 25 4
gpt4 key购买 nike

我的文件包含 100 行。我设置了 jinja2 的所有初始化。我可以将文件呈现到我的浏览器,但问题是它没有格式化。(即行不是一行一行的)。

所以我想使用 jinja2 以格式化的方式(即在浏览器中逐行)呈现文件。我需要在 html 文件和 python 代码中进行哪些编辑。

最佳答案

添加"<br/>"到行尾以便浏览器换行?

这就是你要的吗?

[编辑]

在 python 中读取对象的行:

lines = open("myfile.txt")
for line in lines:
print line.rstrip() //rstrip removes whitespaces at the end
lines.close()

jinja2 逐行打印模板

<title>{% filename %}</title>
<body>
{% for line in lines %}
{{ line }}<br/>
{% endfor %}
</body>

关于python - 我如何使用 jinja2 逐行将文件呈现为 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14744117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com