Jinja2在python列表中循环,以xml形式返回每个项目

Jinja2在python列表中循环,以xml形式返回每个项目,python,xml,python-3.x,jinja2,Python,Xml,Python 3.x,Jinja2,我有一个python列表返回['a','b','c'],我想使用这个列表使用Jinja2模板分别显示每个项目 我尝试过使用batch1: {% for resource in resourcelist|batch(1) %} <file {{ resource }} > {% endfor %} 我想要的输出是: <resource identifier="resource" type="webcontent" adlcp:scormtype="sc

我有一个python列表返回['a','b','c'],我想使用这个列表使用Jinja2模板分别显示每个项目

我尝试过使用batch1:

{% for resource in resourcelist|batch(1) %}
            <file {{ resource }} >
{% endfor %}
我想要的输出是:

<resource identifier="resource" type="webcontent" 
adlcp:scormtype="sco" href="df/res/test.hml">

        <file df/res/test.hml >

        <file ['a'] >

        <file ['b'] >

        <file ['c'] >

</resource>
我对Jinja模板是全新的


我的目标是能够从目录中提取所有文件,并在XML页面中使用Jinja2模板列出它们。

考虑避免在Python中重复分配索引,只需在Jinja中两次引用starting_资源。下面还为格式良好的XML输出正确使用了属性和结束标记

Python脚本不需要mylist变量

输出=模板。渲染开始资源=索引,资源列表=所有资源 金贾模板


向我们展示resourcelist是如何填充的。这听起来像是一个列表列表。@约翰戈顿我从python代码中添加了template.render部分,突出显示了resourcelist的创建,它是一个列表列表。好的,resourcelist确实是一个列表列表。因此,当您在模板中对其进行迭代时,得到的每个值本身也是一个列表。也许您需要在主循环中使用另一个循环{%for file in resource%}?如果您尝试使用XML文件,这不是一个格式良好的输出,因为标记不闭合,并且奇怪地使用方括号,[和],既不反映属性值,也不反映元素值。这对我来说非常有用,因此,我看到了我遇到的问题,因为我的for循环运行在两个列表中,将它们分开允许循环只循环列表本身。非常感谢。
<resource identifier="resource" type="webcontent" 
adlcp:scormtype="sco" href="df/res/test.hml">

        <file df/res/test.hml >

        <file ['a', 'b', 'c'] >

</resource>
mylist = [index, all_resources]

output = template.render(starting_resource = index, resourcelist = 
mylist)
<resource identifier="resource" type="webcontent" 
adlcp:scormtype="sco" href="df/res/test.hml">

        <file df/res/test.hml >

        <file ['a'] >

        <file ['b'] >

        <file ['c'] >

</resource>
<resources>
    <resource identifier="resource" type="webcontent" adlcp:scormtype="sco" 
              href="{{starting_resource}}">
            <file href="{{starting_resource}}" /> 
      {% for resource in resourcelist %}
            <file href="{{resource}}" />
      {% endfor %}
    </resource>
</resources>