[英]Extract ids from OpenStreetMap Overpass API output (python list in dictionary)
I'm querying OpenStreetMaps' Overpass Turbo API ( Link ) with Python 我正在用Python查询OpenStreetMaps的Overpass Turbo API( Link )
import requests
import json
lat='51.404924'
lon='12.444895'
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = "[out:json][timeout:600];(way(around:700,"+lat+","+lon+")[landuse=industrial];);out geom;"
response = requests.get(overpass_url,
params={'data': overpass_query})
data = response.json()
for key in data:
print("dict has", key)
elements=data["elements"]
print(type(elements))
and getting this result (shortened): 并获得此结果(缩短):
{
"version": 0.6,
"generator": "Overpass API 0.7.55 579b1eec",
"elements": [
{
"type": "way",
"id": 12805981,
"geometry": [
{
"lat": 51.410593,
"lon": 12.4327856
},
],
"tags": {
"addr:housenumber": "1",
"addr:street": "BMW-Allee",
"landuse": "industrial",
"name": "BMW Werk 07.10 Leipzig",
}
}
]
}
How can I get the name and id ? 如何获得名称和ID ? I've thought, that dictionaries can't contain lists , but here it does indeed.
我以为,字典不能包含list ,但在这里确实如此。 Because of this my initial idea
因此我的最初想法
print(data["elements"]["tags"]["name"])
doesn't work. 不起作用。
Thanks for your help (this is my first question ;)) 谢谢您的帮助(这是我的第一个问题;)
print(data["elements"][0]["tags"]["name"])
print(data['elements'][0]['id'])
Index into the elements array. 索引到elements数组中。
Dictionaries cannot have lists for keys, but can have lists for values. 词典不能包含键列表,但可以包含值列表。
You can use a tuple as a key in a dictionary. 您可以将元组用作字典中的键。 Basically anything immutable can be a key in a dict.
基本上,任何不变的东西都可以成为字典中的关键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.