Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

google maps - Reverse geocoding with Python/GoogleMaps API: How to parse response

I'm attempting to use the Google Maps Geocoding API to find the State associated with a latitude and longitude with a Python script. I have no trouble getting back a response from the server, but when I do, I'm having trouble extracting the information I need.

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("
")
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'])

The response I receive back is:

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

What I don't get is WHY it works fine when I ask it to print geocode[0]['address_components'][0]["types"], but complains I'm treating a list like a dictionary when I try geocode[0]['address_components'][i]['types'] .

Any help would be greatly appreciated, and thank you all so much for your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You error is with python syntax:

Code resolved

for x in geocode[0]['address_components']:
    print(x)

    if 'administrative_area_level_1' in x['types'] :
        print(x['long_name'])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...