python 字典格式化字符串的问题
PHP中文网
PHP中文网 2017-04-17 15:35:57
0
2
775
>>> "%(1)s" % {1:'a',2:'b'}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '1'

下面这个就可以,为什么?

>>> "%(1)s" % {'1':'a','2':'b'}
'a'
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
伊谢尔伦

Change to %d and try again

阿神

KeyError should be of typeError.
I tried

"%(1)s" % {'3': 'a', '2': 'b'}

Still report the original error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '1'

I saw that the %(1)s in this 1 is to match the key value.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template