Gal*_*len 3 python attributes json
敬启者,
我的代码错误有什么不对?
>>> import json
>>> array = json.load({"name":"Galen","learning objective":"load json files for data analysis"})
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
array = json.load({"name":"Galen","learning objective":"load json files for data analysis"})
File "C:\Python34\lib\json\__init__.py", line 265, in load
return loads(fp.read(),
AttributeError: 'dict' object has no attribute 'read'
Run Code Online (Sandbox Code Playgroud)
Arc*_*rma 16
由于您希望将其转换为json格式,因此您应该使用json.dumps()而不是json.load().这可行:
>>> import json
>>> array = json.dumps({"name":"Galen","learning objective":"load json files for data analysis"})
>>> array
'{"learning objective": "load json files for data analysis", "name": "Galen"}'
Run Code Online (Sandbox Code Playgroud)
输出:
>>> a = json.loads(array)
>>> a["name"]
u'Galen'
Run Code Online (Sandbox Code Playgroud)