【发布时间】:2021-08-13 03:19:33
【问题描述】:
我正在使用 jinja 模板来打印字典的键。
代码如下:
from jinja2 import Template
import json
data = '''
hello {{Names}}
Heading is {{ Names.keys() }}
'''
schema = '''
{
"Names" : [
"Name1",
"Name2",
"Name3"
]
}
'''
k = json.loads(schema)
tm = Template(data)
jdata = tm.render(Names=k)
print(jdata)
它打印模板为dict_keys,见下面的输出:
hello {'Names': ['Name1', 'Name2', 'Name3']}
Heading is dict_keys(['Names'])
所以,我认为dict_keys 是不支持索引的set 类型,而且我无法使用list 方法(通常在python 中使用)将其转换为列表然后使用索引.
我想把它打印成一个字符串,预期的输出:
hello {'Names': ['Name1', 'Name2', 'Name3']}
Heading is Names # see the Names, it is string
【问题讨论】:
-
什么是名称有多个键?
-
你期待什么结果?我不完全清楚你想要的输出是什么。
标签: python python-3.x dictionary jinja2