使用 Python/GoogleMaps API 进行反向地理编码:如何解析响应
Reverse geocoding with Python/GoogleMaps API: How to parse response
我正在尝试使用 Google 地图地理编码 API 通过 Python 脚本查找与纬度和经度相关联的州。我可以轻松地从服务器取回响应,但当我这样做时,我无法提取我需要的信息。
This is my code:
from pymongo import MongoClient
import googlemaps
client = MongoClient()
turnkey = "MYVERYSECRETAPICODEWOOOOOOOOO"
gmaps = googlemaps.Client(key=turnkey)
lat = 29
long = -98.4936
#This code runs fine:
geocode = gmaps.reverse_geocode((lat, long))
print(geocode[0]['address_components'])
print("\n")
print(geocode[0]['address_components'][0])
print(geocode[0]['address_components'][0]["types"])
#This Code Is a Problem:
for i in geocode[0]['address_components'][:]:
print(geocode[0]["address_components"][i])
if 'administrative_area_level_1' in geocode[0]['address_components'][i]['types'] :
print(geocode[0]['address_components'][i]['long_name'])
我收到的回复是:
Traceback (most recent call last):
[{'long_name': '6476', 'short_name': '6476', 'types': ['street_number']}, {'long_name': 'East Fm 476', 'short_name': 'E Fm 476', 'types': ['route']}, {'long_name': 'Pleasanton', 'short_name': 'Pleasanton', 'types': ['locality', 'political']}, {'long_name': 'Atascosa County', 'short_name': 'Atascosa County', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Texas', 'short_name': 'TX', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}, {'long_name': '78064', 'short_name': '78064', 'types': ['postal_code']}]
File "C:/Users/Pendelluft/Documents/CodeyMcCodeFace/CodeyCode/SNAKESSSSS/SingleQuery.py", line 24, in <module>
print(geocode[0]["address_components"][i])
{'long_name': '6476', 'short_name': '6476', 'types': ['street_number']}
['street_number']
TypeError: list indices must be integers or slices, not dict
Process finished with exit code 1
我不明白的是为什么当我要求它打印 geocode[0]['address_components'][0]["types"] 时它工作正常,但抱怨我正在治疗当我尝试 geocode[0]['address_components'][i]['types'] 时,列表就像字典一样。
任何帮助将不胜感激,非常感谢大家抽出宝贵时间。
你的错误是 python 语法:
代码已解决
for x in geocode[0]['address_components']:
print(x)
if 'administrative_area_level_1' in x['types'] :
print(x['long_name'])
我正在尝试使用 Google 地图地理编码 API 通过 Python 脚本查找与纬度和经度相关联的州。我可以轻松地从服务器取回响应,但当我这样做时,我无法提取我需要的信息。
This is my code:
from pymongo import MongoClient
import googlemaps
client = MongoClient()
turnkey = "MYVERYSECRETAPICODEWOOOOOOOOO"
gmaps = googlemaps.Client(key=turnkey)
lat = 29
long = -98.4936
#This code runs fine:
geocode = gmaps.reverse_geocode((lat, long))
print(geocode[0]['address_components'])
print("\n")
print(geocode[0]['address_components'][0])
print(geocode[0]['address_components'][0]["types"])
#This Code Is a Problem:
for i in geocode[0]['address_components'][:]:
print(geocode[0]["address_components"][i])
if 'administrative_area_level_1' in geocode[0]['address_components'][i]['types'] :
print(geocode[0]['address_components'][i]['long_name'])
我收到的回复是:
Traceback (most recent call last):
[{'long_name': '6476', 'short_name': '6476', 'types': ['street_number']}, {'long_name': 'East Fm 476', 'short_name': 'E Fm 476', 'types': ['route']}, {'long_name': 'Pleasanton', 'short_name': 'Pleasanton', 'types': ['locality', 'political']}, {'long_name': 'Atascosa County', 'short_name': 'Atascosa County', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Texas', 'short_name': 'TX', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}, {'long_name': '78064', 'short_name': '78064', 'types': ['postal_code']}]
File "C:/Users/Pendelluft/Documents/CodeyMcCodeFace/CodeyCode/SNAKESSSSS/SingleQuery.py", line 24, in <module>
print(geocode[0]["address_components"][i])
{'long_name': '6476', 'short_name': '6476', 'types': ['street_number']}
['street_number']
TypeError: list indices must be integers or slices, not dict
Process finished with exit code 1
我不明白的是为什么当我要求它打印 geocode[0]['address_components'][0]["types"] 时它工作正常,但抱怨我正在治疗当我尝试 geocode[0]['address_components'][i]['types'] 时,列表就像字典一样。
任何帮助将不胜感激,非常感谢大家抽出宝贵时间。
你的错误是 python 语法:
代码已解决
for x in geocode[0]['address_components']:
print(x)
if 'administrative_area_level_1' in x['types'] :
print(x['long_name'])