如何解决“ dict”对象没有属性“ write”的问题?

时间:2019-11-21 17:12:50

标签: python json url

我以前从未使用过JSON文件,并且我认为也许我对如何提取数据有些迷茫。这是我的代码:

# Loads data on women breastfeeding in America from the web # 
import urllib.request
import json 
with urllib.request.urlopen("https://chronicdata.cdc.gov/views/8hxn-cvik/rows.json?accessType=DOWNLOAD") as web_data:
    data = json.loads(web_data.read().decode())
    json.dump(data, web_data)

由于链接本身是网页视图,因此我试图从Internet提取数据,而我的计算机拒绝让我将其保存到计算机上。我希望能够处理数据,但是遇到了一些问题。使用上面的代码,我得到以下错误:

---------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-31-7f3487bc83ea> in <module>()
      6     data = json.loads(web_data.read().decode())
      7     #write_data=data.write()
----> 8     json.dump(data, web_data)

~\OneDrive\Documents\Python stuff\Pythonstuff\lib\json\__init__.py in dump(obj, fp, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    178     # a debuggability cost
    179     for chunk in iterable:
--> 180         fp.write(chunk)
    181 
    182 

UnsupportedOperation: write

我尝试了以下操作,认为与.read()有关:

import urllib.request
import json 
with urllib.request.urlopen("https://chronicdata.cdc.gov/views/8hxn-cvik/rows.json?accessType=DOWNLOAD") as web_data:
    data = json.loads(web_data.read().decode())
    write_data=data.write()
    json.dump(write_data, web_data)

然后出现以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-32-7ce187449c36> in <module>()
      5 with urllib.request.urlopen("https://chronicdata.cdc.gov/views/8hxn-cvik/rows.json?accessType=DOWNLOAD") as web_data:
      6     data = json.loads(web_data.read().decode())
----> 7     write_data=data.write()
      8     json.dump(write_data, web_data)

AttributeError: 'dict' object has no attribute 'write'

有人可以帮我指出正确的方向吗?我对如何打开此文件并开始处理数据一无所知。我最终将希望能够从那里将某些东西处理到一个数据框中。

1 个答案:

答案 0 :(得分:3)

如果我正确理解了您的问题,您想将数据写入文件并将其保存在计算机中,对吗?然后,您应该打开要写入的文件(在这里我称其为rows.json):

import urllib.request
import json

with urllib.request.urlopen("https://chronicdata.cdc.gov/views/8hxn-cvik/rows.json?accessType=DOWNLOAD") as web_data:
    data = json.loads(web_data.read().decode())
    with open("rows.json", "w") as out_file:
        json.dump(data, out_file)