Last active
May 17, 2019 12:40
-
-
Save thilohuellmann/27bee7db4729d10ae8e75a710cf5c1c7 to your computer and use it in GitHub Desktop.
medium4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for query in tqdm(addresses): | |
# API call, storing information as JSON | |
url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + query + '&lang=en&key=' + api_key | |
r = requests.get(url) | |
data = r.json() | |
# clear all values to avoid appending values from previous iterations a second time | |
number = street = country = postal_code = city = '' | |
# looping over address components in JSON | |
for component in data['results'][0]['address_components']: | |
if 'street_number' in component['types']: | |
number = component['long_name'] | |
elif 'route' in component['types']: | |
street = component['long_name'] | |
elif 'country' in component['types']: | |
country = component['long_name'] | |
elif 'postal_code' in component['types']: | |
postal_code = component['long_name'] | |
elif 'locality' in component['types']: | |
city = component['long_name'] | |
elif 'postal_town' in component['types']: | |
city = component['long_name'] | |
else: | |
continue | |
street_and_no = street + ' ' + number | |
transformed.append([country, postal_code, city, street_and_no]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i am getting an error after running this code:
IndexError Traceback (most recent call last)
in ()
11 # looping over address components in JSON
12 #for component in ($result['results'][0]['address_components'] as $key => $val)
---> 13 for component in data['results'][0]['address_components']:
14 if 'street_number' in component['types']:
15 number = component['long_name']
IndexError: list index out of range
please help