擅长:python、mysql、java
<p>可能最常见的方法是把每一条记录都放入一个dict中</p>
<pre><code>info = []
for r in information:
record = dict(name=r[0], location=r[1], region=r[2], idcode=r[3])
info.append(record)
</code></pre>
<p>然后,Jinja2允许您使用x.name等访问属性,就像您在示例中所做的那样。你知道吗</p>
<pre><code>{% for x in info %}
<tr>
<td>{{x.name}}</td>
<td>Via: {{x.location}} | Loop: {{x.region}}</td>
<td>{{x.idcode}}</td>
</tr>
{% endfor %}
</code></pre>
<p>注意,这种索引到数据(x.name)的方法是一种特定于jinja2的快捷方式(尽管它是从django模板偷来的,而django模板可能是从其他地方偷来的)。你知道吗</p>
<p>在python本身中,您必须:</p>
<pre><code>for x in info:
print(x['name'])
# x.name will throw an error since name isn't an 'attribute' within x
# x['name'] works because 'name' is a 'key' that we added to the dict
</code></pre>