Skip to content Skip to sidebar Skip to footer

Iterating Through Geocoding Api Response In Python

I'm trying to find the ['locality', 'political'] value in the reverse geocoding Google Maps API. I could achieve the same in Javascript as follows: var formatted = data.results; $

Solution 1:

You are trying to access the keys of a dictionary, but instead you are iterating over the characters of that key:

forcinresults:
    ifc['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...

Most likely you wanted:

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

Post a Comment for "Iterating Through Geocoding Api Response In Python"