我正在使用Python 3.52
并且我有两个键用于dict中的单个值:
代码:
for key, value in unused_reserved_instances.items():
print("Key: ", key)
print("Value: ", value)
输出:
Key: ('m3.large', 'us-west-2b')
Value: 1
Key: ('m3.xlarge', 'us-west-2b')
Value: 1
jinja模板:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 3px;
text-align: center;
}
table#t01 {
width: 30%;
background-color: #f1f1c1;
}
</style>
</head>
<body>
<table id="t01">
<tr>
<th>Instance Type</th>
<th>Count</th>
</tr>
{% for key, value in unused_reserved_instances.items() %}
<tr>
<td>{{key}}</td>
<td>{{value}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
而不是:
Instance Type Count
('m3.large', 'us-west-2b') 1
('m3.xlarge', 'us-west-2b') 1
我希望它类似于以下示例之一(只是没有引号和括号):
Instance Type Count
m3.large, us-west-2b 1
m3.xlarge, us-west-2b 1
或者也许是这样:
Instance Type Count
m3.large - us-west-2b 1
m3.xlarge - us-west-2b 1
由于
答案 0 :(得分:1)
您可以像这样加入元组中的字符串:
{% for key, value in unused_reserved_instances.items() %}
<tr>
<td>{{key|join(' - ')}}</td>
<td>{{value}}</td>
</tr>
{% endfor %}
答案 1 :(得分:1)
你可以迭代那个元组:
<td>{% for v in key %}{{v}}{%endfor%}</td>