Skip to content

Instantly share code, notes, and snippets.

@thilohuellmann
Last active May 17, 2019 12:40
Show Gist options
  • Save thilohuellmann/27bee7db4729d10ae8e75a710cf5c1c7 to your computer and use it in GitHub Desktop.
Save thilohuellmann/27bee7db4729d10ae8e75a710cf5c1c7 to your computer and use it in GitHub Desktop.
medium4
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])
@Sweetydevassy
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment