【问题标题】:Python/Flask/HTML <plaintext> Output is adding spaces [duplicate]Python/Flask/HTML <plaintext> 输出添加空格 [重复]
【发布时间】:2016-11-15 19:16:21
【问题描述】:

所以我使用 python 烧瓶将脚本的结果输出到网页上的纯文本。当我从 CLI 打印函数时,输出如下所示:

1
2
3
4
5

但是,当它在网站上时,它看起来像这样:

1

2

3

4

5

输出的 HTML 文件如下所示:

<plaintext>
{% for rows in report %}
    {{rows}}
{% endfor %}

我真的不擅长 HTML,但是有没有办法删除添加的新行?

【问题讨论】:

    标签: python html flask


    【解决方案1】:
    1. &lt;plaintext&gt; 已过时。请改用&lt;pre&gt;。见:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/plaintext

    2. 您使用的rows 变量可能已经包含换行符。如果您只想输出report 中的每一行,则不需要使用 for 循环。试试这个:

      <pre>{{ ''.join(report) }}</pre>
      
    3. 考虑控制结构。您可以使用 Jinja2 的 whitespace control 功能来避免多余的空格(和换行符):

      <pre>
      {% for row in report -%}
          {{ row }}
      {%- endfor %}
      </pre>
      

    【讨论】:

    • 谢谢!这很好用,抱歉重复了,我搜索时没有找到那个。
    猜你喜欢
    • 2018-08-11
    • 2017-09-14
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2021-11-28
    • 2011-02-26
    相关资源
    最近更新 更多