我正在使用Python 3.52并且在字典中有2个键指向单个值:

码:

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


谢谢

最佳答案

您可以像这样将字符串加入元组中:

{% for key, value in unused_reserved_instances.items() %}
  <tr>
    <td>{{key|join(' - ')}}</td>
    <td>{{value}}</td>
  </tr>
{% endfor %}

关于python - 如何删除在Jinja模板中具有两个指向一个值的键的字典中的引号和括号?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41780715/

10-12 23:11