我通过results = requests.get(url).json()
获得了结果结果看起来像这样:
{'type':'FeatureCollection','crs':{'type':'name','properties':{'name':'EPSG:4326'}},'features':[{'type':'Feature','properties':{'kode':'0101','navn':'København','region_kode':'1084.0','region_navn':'Hovedstaden'},'bbox':[12.453042062098154,55.612994971371606,12.734252598475942,55.732491190632494]}]}
具有结果['功能'],我得到了这个
[{'type':'Feature','properties':{'kode':'0101','navn':'København','region_kode':'1084.0','region_navn':'Hovedstaden'},'bbox':[12.453042062098154,55.612994971371606,12.734252598475942,55.732491190632494]}]
我想在“ navn”中获取信息我尝试了
的所有组合results['features']['properties']['navn']
results['features']['navn']
results['features']['properties']
它们都显示相同的错误消息:列表索引必须是整数或切片,而不是str
显然,results ['features']是一个长度为1的列表。
我如何获得“导航”信息?
我想打个电话,你可以想像。
results['features']
对象是一个列表,请尝试results['features'][0]['properties']['navn']
现在选择列表(0)中的第一个元素,即字典,然后从该字典中选择'navn'键。结果是'navn'
的值请注意,python列表在[]之间,并且项目之间用逗号分隔,而python字典在{}之间,并且由键,值对组成,并用逗号分隔。
尝试一下
results['features'][0]['properties']['navn']
您可以在下面尝试代码:
results['features'][0]['properties']['navn']
您应该尝试访问list
中result['features']
的第一个元素,即:
results['features'][0]['properties']['navn']
完整代码:
results = {'type': 'FeatureCollection', 'crs': {'type': 'name', 'properties': {'name': 'EPSG:4326'}}, 'features': [{'type': 'Feature',
'properties': {'kode': '0101', 'navn': 'København', 'region_kode': '1084.0', 'region_navn': 'Hovedstaden'}, 'bbox': [12.453042062098154, 55.612994971371606, 12.734252598475942, 55.732491190632494]}]}
print(results['features'][0]['properties']['navn'])
# København