本文介紹了'dict' 對象沒有屬性 'read'的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在 Windows 系統上運行 Python 我遇到了將 JSON 文件加載到內存中的問題.我的代碼有什么問題?
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({"name":"Name","learning objective":"load json files for data analysis"})
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
array = json.load({"name":"Name","learning objective":"load json files for data analysis"})
File "C:Python34libjson\__init__.py", line 265, in load
return loads(fp.read(),
AttributeError: 'dict' object has no attribute 'read'
推薦答案
既然要轉成json
格式,就用json.dumps()
代替json.load()
.這會起作用:
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"}'
輸出:
>>> a = json.loads(array)
>>> a["name"]
u'Galen'
這篇關于'dict' 對象沒有屬性 'read'的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!