可能涉及的人,
我的代码出了什么问题?
>>> 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'
答案 0 :(得分:13)
由于您希望将其转换为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"}'
<强>输出:强>
>>> a = json.loads(array)
>>> a["name"]
u'Galen'
答案 1 :(得分:0)
我想你是在追求这个:
import json
array = json.dumps({"name":"Galen","learning objective":"load json files for data analysis"})
print(array)
给出:
{"learning objective": "load json files for data analysis", "name": "Galen"}
答案 2 :(得分:0)
正如你所说,这是错误的,你忘记了json文本之前和之后。
import json
array = json.load('{"name":"Galen","learning objective":"load json files for data analysis"}')
我犯了同样的错误:)
转储有效,但不一样。加载更适合解析json。 https://docs.python.org/2/library/json.html