希望从url访问特定的字典值,并在python中获取键0错误
我想访问希望从url访问特定的字典值,并在python中获取键0错误,python,Python,我想访问URL 我编写了以下代码,但它抛出了一个错误键0。我想从URL中访问一些值,如位置、标题、几何体,并将其写入CSV文件 import urllib2 import json url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-10-01&endtime=2016-10-02' # download the json string json_stri
URL
我编写了以下代码,但它抛出了一个错误键0。我想从URL
中访问一些值,如位置、标题、几何体,并将其写入CSV
文件
import urllib2
import json
url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-10-01&endtime=2016-10-02'
# download the json string
json_string = urllib2.urlopen(url).read()
# de-serialize the string so that we can work with it
j = json.loads(json_string)
names = [d['properties'] for d in j[0]['type']]
print names
我是python新手。
j
是一个字典,它有键['type'、'metadata'、'features'、'bbox']
,但没有0。您可能正在查找j['features']
,而不是j[0]['type']]
,但是名称的值是词典列表,而不是名称。我假设该站点的JSON API自您(或编写代码的人)上次使用以来已经发生了变化。因为您可以调用打印名称
,所以您可能正在使用python 2。尝试使用Python3,因为它目前得到了更好的支持。您的错误可能与您试图与Python2一起使用的Python3教程或包有关。