I am using the googlemaps library to query the google maps api.I would like to extract the name of a City based on zip code.
For example, for zip code 23325 I would like to extract Chesapeake.
Here is my initial code:
import googlemaps
from keys import google_api_key
gmaps = googlemaps.Client(key=google_api_key)
geocode_result = gmaps.geocode('23325')
print(geocode_result)
Here are the results from printing the geocode_results:
[{'address_components': [{'long_name': '23325', 'short_name': '23325', 'types': ['postal_code']}, {'long_name': 'Indian River', 'short_name': 'Indian River', 'types': ['neighborhood', 'political']}, {'long_name': 'Chesapeake', 'short_name': 'Chesapeake', 'types': ['locality', 'political']}, {'long_name': 'Virginia', 'short_name': 'VA', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}], 'formatted_address': 'Chesapeake, VA 23325, USA', 'geometry': {'bounds': {'northeast': {'lat': 36.8399469, 'lng': -76.21933109999999}, 'southwest': {'lat': 36.7870261, 'lng': -76.25747489999999}}, 'location': {'lat': 36.8179707, 'lng': -76.2305308}, 'location_type': 'APPROXIMATE', 'viewport': {'northeast': {'lat': 36.8399469, 'lng': -76.21933109999999}, 'southwest': {'lat': 36.7870261, 'lng': -76.25747489999999}}}, 'place_id': 'ChIJTcQMfRK9uokRcVFVU9zMizw', 'types': ['postal_code']}]
When I look at the type
print(type(geocode_result))
Python tells me
<class 'list'>
If I try anything like geocode_result[1] I get the following error message:
IndexError: list index out of range
If I try geocode_result['address_components'] I get a TypeError: list indices must be integers or slices, not str message
Any help would be greatly appreciated! Thank you!
geocode_result[0]?