问题描述
我想访问在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的新手。
1楼
j
是一个具有键['type', 'metadata', 'features', 'bbox']
,但不是 0。您可能正在寻找j['features']
,而不是j[0]['type']
,但是names
的值是一个字典列表,而不是names 。
我假设该站点的 JSON API 自从您(或任何编写代码的人)上次使用它以来已经发生了变化。