<p>用字符串格式化dict键的正确方法是什么?</p>
<p>当我这样做时:</p>
<pre><code>>>> foo = {'one key': 'one value', 'second key': 'second value'}
>>> "In the middle of a string: {foo.keys()}".format(**locals())
</code></pre>
<p>我期望的是:</p>
<pre><code>"In the middle of a string: ['one key', 'second key']"
</code></pre>
<p>我得到的是:</p>
<pre><code>Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
"In the middle of a string: {foo.keys()}".format(**locals())
AttributeError: 'dict' object has no attribute 'keys()'
</code></pre>
<p>但正如你所见,我的字典有钥匙:</p>
<pre><code>>>> foo.keys()
['second key', 'one key']
</code></pre>