Skip to content Skip to sidebar Skip to footer

'dict' Object Has No Attribute 'read'

Running Python on a Windows system I encountered issues with loading a JSON file into memory. What is wrong with my code? >>> import json >>> array = json.load({'

Solution 1:

Since you want to convert it into json format, you should use json.dumps() instead of json.load(). This would work:

>>>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"}'

Output:

>>> a = json.loads(array)
>>> a["name"]
u'Galen'

Solution 2:

if you want to load json from a string you need to add quotes around your string and there is a different method to read from file or variable. For variable it ends with "s" other doesn't

import json

my_json = '{"my_json" : "value"}'

res = json.loads(my_json)
print res

Solution 3:

As you said, it is wrong, you forgot the ' before and after the json text.

import json
array = json.load('{"name":"Galen","learning objective":"load json files for data analysis"}')

I had the same mistake :)

dumps works but it is not the same. Load is better for parsing json. https://docs.python.org/2/library/json.html

Post a Comment for "'dict' Object Has No Attribute 'read'"