When I run this code attached to CSV with URL placed at columnA[1] and inserting the names at A[2] getting the following error:
IndexError: List Index out of range
The goal is to extract the location by giving names in CSV format.
# importing the requests library
import requests
import csv
# api-endpoint
URL = "http://maps.googleapis.com/maps/api/geocode/json"
f = open("z1.csv")
reader = csv.DictReader(f)
for row in reader:
print(row["URL"])
# location given here
location = row["URL"]
records = location.splitlines()
myreader = csv.reader(records)
for row in myreader:
print('Location', row[0])
# Defining a params dict for the parameters to be sent to the API
PARAMS = {'address':location}
# Sending get request and saving the response as response object
r = requests.get(url = URL, params = PARAMS)
# Extracting data in json format
data = r.json()
# Extracting latitude, longitude and formatted address
# of the first matching location
latitude = data['results'][0]['geometry']['location']['lat']
longitude = data['results'][0]['geometry']['location']['lng']
formatted_address = data['results'][0]['formatted_address']
# Printing the output
print("Latitude:%s\nLongitude:%s\nFormatted Address:%s"
%(latitude, longitude,formatted_address))