在python中迭代Geocoding API响应 [英] Iterating through Geocoding API response in python
问题描述
我试图在反向地理编码Google Maps API中找到 ['locality','political']
值。
我可以在Javascript中实现如下功能:
var formatted = data.results;
$ .each(格式化,函数(){
if(this.types.length!= 0){
if(this.types [0] =='locality'&& amp ; this.types [1] =='political'){
address_array = this.formatted_address.split(',');
} else {
// somefunction
}
} else {
// somefunction
}
});
使用Python,我尝试了以下方法:
url ='https://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&result_type=locality&key ='+ MAPS_API_KEY
results = json.loads(urllib.request.urlopen(url).read()。decode('utf-8'))
city_components = results ['results'] [0 ]
for c in results:
if c ['types']:
if c ['types'] [0] =='locality':
print(c [ 'types'])
这给我一堆错误。我无法绕过响应对象来查找 ['locality','political']
值来查找相关City SHORT_NAME
。如何解决这个问题?
您试图访问字典的键,但是您正在迭代该键的字符:
结果中的c:
if c ['types']:
最有可能你想要: I'm trying to find the I could achieve the same in Javascript as follows: Using Python, I tried the following: This gives me a bunch of errors. I'm not able to get my head around iterating through the response object to find the You are trying to access the keys of a dictionary, but instead you are iterating over the characters of that key: Most likely you wanted:
这篇关于在python中迭代Geocoding API响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!结果
是一个字典(从 city_components =
line)。当你在结果中键入 for c时,你将
c
绑定到该字典的键(依次)。这意味着 c
是一个字符串(在你的场景中,很可能所有的键都是字符串)。因此,然后键入 c ['types']
没有意义:您正尝试访问值/属性'types'
一个字符串...
在结果['results']中选择:
addr_comp = option.get('address_components',[])
用于addr_comp中的address_type:
flags = address_type.get('types ',[])
如果标志中的'locality'和标志中的'political':
print(address_type ['short_name'])
['locality', 'political']
value in the reverse geocoding Google Maps API. var formatted = data.results;
$.each(formatted, function(){
if(this.types.length!=0){
if(this.types[0]=='locality' && this.types[1]=='political'){
address_array = this.formatted_address.split(',');
}else{
//somefunction
}
}else{
//somefunction
}
});
url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&result_type=locality&key='+MAPS_API_KEY
results = json.loads(urllib.request.urlopen(url).read().decode('utf-8'))
city_components = results['results'][0]
for c in results:
if c['types']:
if c['types'][0] == 'locality':
print(c['types'])
['locality', 'political']
value to find the related City short_name
. How do I go about solving this?for c in results:
if c['types']:
results
is a dictionary (obvious from your city_components=
line). When you type for c in results
, you are binding c
to the keys of that dictionary (in turn). That means c
is a string (in your scenario, most likely all the keys are strings). So then typing c['types']
does not make sense: you are trying to access the value/attribute 'types'
of a string...for option in results['results']:
addr_comp = option.get('address_components', [])
for address_type in addr_comp:
flags = address_type.get('types', [])
if 'locality' in flags and 'political' in flags:
print(address_type['short_name'])