Formatting dict keys: AttributeError: 'dict' object has no attribute 'keys()'
Formatting dict keys: AttributeError: 'dict' object has no attribute 'keys()'
在字符串中格式化字典键的正确方法是什么?
当我这样做时:
>>> foo = {'one key': 'one value', 'second key': 'second value'}
>>> "In the middle of a string: {foo.keys()}".format(**locals())
我的期望:
"In the middle of a string: ['one key', 'second key']"
我得到的:
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()'
但是如你所见,我的字典有键:
>>> foo.keys()
['second key', 'one key']
您不能在占位符中调用方法。您可以访问属性和特性,甚至可以索引值 - 但您不能调用方法:
class Fun(object):
def __init__(self, vals):
self.vals = vals
@property
def keys_prop(self):
return list(self.vals.keys())
def keys_meth(self):
return list(self.vals.keys())
方法示例(失败):
>>> foo = Fun({'one key': 'one value', 'second key': 'second value'})
>>> "In the middle of a string: {foo.keys_meth()}".format(foo=foo)
AttributeError: 'Fun' object has no attribute 'keys_meth()'
属性 示例(有效):
>>> foo = Fun({'one key': 'one value', 'second key': 'second value'})
>>> "In the middle of a string: {foo.keys_prop}".format(foo=foo)
"In the middle of a string: ['one key', 'second key']"
格式语法清楚地表明您只能访问属性(la getattr
)或索引(la __getitem__
)占位符(取自 "Format String Syntax"):
The arg_name can be followed by any number of index or attribute expressions. An expression of the form '.name'
selects the named attribute using getattr()
, while an expression of the form '[index]'
does an index lookup using __getitem__()
.
使用 Python 3.6 你可以很容易地用 f-strings 做到这一点,你甚至不必传入 locals
:
>>> foo = {'one key': 'one value', 'second key': 'second value'}
>>> f"In the middle of a string: {foo.keys()}"
"In the middle of a string: dict_keys(['one key', 'second key'])"
>>> foo = {'one key': 'one value', 'second key': 'second value'}
>>> f"In the middle of a string: {list(foo.keys())}"
"In the middle of a string: ['one key', 'second key']"
"In the middle of a string: {}".format(list(foo.keys()))
"In the middle of a string: {}".format([k for k in foo])
正如上面其他人所说,您无法按照自己喜欢的方式进行操作,这里有一些额外的信息需要遵循python string format calling a function
在字符串中格式化字典键的正确方法是什么?
当我这样做时:
>>> foo = {'one key': 'one value', 'second key': 'second value'}
>>> "In the middle of a string: {foo.keys()}".format(**locals())
我的期望:
"In the middle of a string: ['one key', 'second key']"
我得到的:
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()'
但是如你所见,我的字典有键:
>>> foo.keys()
['second key', 'one key']
您不能在占位符中调用方法。您可以访问属性和特性,甚至可以索引值 - 但您不能调用方法:
class Fun(object):
def __init__(self, vals):
self.vals = vals
@property
def keys_prop(self):
return list(self.vals.keys())
def keys_meth(self):
return list(self.vals.keys())
方法示例(失败):
>>> foo = Fun({'one key': 'one value', 'second key': 'second value'})
>>> "In the middle of a string: {foo.keys_meth()}".format(foo=foo)
AttributeError: 'Fun' object has no attribute 'keys_meth()'
属性 示例(有效):
>>> foo = Fun({'one key': 'one value', 'second key': 'second value'})
>>> "In the middle of a string: {foo.keys_prop}".format(foo=foo)
"In the middle of a string: ['one key', 'second key']"
格式语法清楚地表明您只能访问属性(la getattr
)或索引(la __getitem__
)占位符(取自 "Format String Syntax"):
The arg_name can be followed by any number of index or attribute expressions. An expression of the form
'.name'
selects the named attribute usinggetattr()
, while an expression of the form'[index]'
does an index lookup using__getitem__()
.
使用 Python 3.6 你可以很容易地用 f-strings 做到这一点,你甚至不必传入 locals
:
>>> foo = {'one key': 'one value', 'second key': 'second value'}
>>> f"In the middle of a string: {foo.keys()}"
"In the middle of a string: dict_keys(['one key', 'second key'])"
>>> foo = {'one key': 'one value', 'second key': 'second value'}
>>> f"In the middle of a string: {list(foo.keys())}"
"In the middle of a string: ['one key', 'second key']"
"In the middle of a string: {}".format(list(foo.keys()))
"In the middle of a string: {}".format([k for k in foo])
正如上面其他人所说,您无法按照自己喜欢的方式进行操作,这里有一些额外的信息需要遵循python string format calling a function