如果有一个字典:
dict={'a':'b'}
在Python 3中,我想将此字典键转换为字符串,其值转换为另一个字符串:
print(key)
key='a'
print(type(key))
str
print(value)
value='a'
print(type(value))
str
一些尝试:
str(dict.key()) # returns 'dict' object has no attribute 'key'
json.dump(dict) # returns {'a':'b'} in string, but hard to process
有简单的解决方案吗?谢谢!
keys()
而不是key()
。 - drumprint(str(dict.keys()))
showsa
you need to usekeys()
instead ofkey()
- Omer Tekbiyikdict
作为变量名是相当不幸的,因为dict
是一种类型名称。 - Kuchara