1

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!

2
  • What happens when you try geocode_result[0]? Commented Feb 4, 2018 at 20:04
  • I get the same results as printing geocode_results. I tried using bytes.decode ... but I don't really know what I'm doing. Commented Feb 4, 2018 at 20:08

2 Answers 2

1

You have a list of dicts. So you can get the first item using the list index, and then you get the dict element using a key.

So for example:

item = geocode_result[0]
item['address_components'] # Returns another list
item['formatted_address'] # Returns a string 'Chesapeake, VA 23325, USA'
Sign up to request clarification or add additional context in comments.

1 Comment

Booom! If I had a kid on the way, I would name them after you! Thank you so much! I used your suggestion item = geocode_result[0] and then x = item['formatted_address'] . I then used x2 = x.split(",")[0] to pull out the City name.
0

Thats reverse geocode. This would do the trick :

import urllib, json
import urllib.request
geocode = input("Enter zip code : ")
url = "https://maps.googleapis.com/maps/api/geocode/json?&address=" + geocode
response = urllib.request.urlopen(url)
data = json.loads(response.read())
print(data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.