gpt4 book ai didi

python - 换行符和破折号在神社中无法正常工作

转载 作者:太空狗 更新时间:2023-10-30 02:18:55 26 4
gpt4 key购买 nike

如何生成预期的输出?谢谢

神社模板

{%- for field in fields -%}

-
name: {{field}}
type: string



{%- endfor -%}

输出

-
name: operating revenue
type: string-
name: gross operating profit
type: string-

预期输出

-
name: operating revenue
type: string
-
name: gross operating profit
type: string

代码

from jinja2 import Template

fields = ["operating revenue", "gross operating profit", "EBITDA", "operating profit after depreciation", "EBIT", "date"]
template_file = open('./fields_template.jinja2').read()
template = Template(template_file)
html_rendered = template.render(fields=fields)
print(html_rendered)

最佳答案

- 删除 Jinja 标签的那一侧和第一个字符之间的所有空格。您在标签的“内部”使用 -,因此删除了 - 字符和单词 string 之后的空格,加入上两个。删除一个或另一个。

例如,您可以删除文本开头和结尾的额外换行符,并从开始标记的内侧删除 -:

{%- for field in fields %}
-
name: {{field}}
type: string
{%- endfor -%}

演示:

>>> from jinja2 import Template
>>> fields = ["operating revenue", "gross operating profit", "EBITDA", "operating profit after depreciation", "EBIT", "date"]
>>> template_file = '''\
... {%- for field in fields %}
... -
... name: {{field}}
... type: string
... {%- endfor -%}
... '''
>>> template = Template(template_file)
>>> html_rendered = template.render(fields=fields)
>>> print(html_rendered)

-
name: operating revenue
type: string
-
name: gross operating profit
type: string
-
name: EBITDA
type: string
-
name: operating profit after depreciation
type: string
-
name: EBIT
type: string
-
name: date
type: string

关于python - 换行符和破折号在神社中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33232830/

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