-3

I found a python code on stack overflow here Reverse geocode, reading .CSV file and TypeError: a float is required

I am trying to reverse geocode my own CSV file. I decided to use (Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32) because I believe that is what the code I'm using was written in. This is the code

from pygeocoder import Geocoder
import csv

gmaps = Geocoder(api_key='api-key')

input = open('C:/Users/DavidDouglas/pythongeocode/BikeEvent_view.csv','r')
output = open('C:/Users/Steffen/pythongeocode/addresses.csv','w')

try:
    reader = csv.reader(input)
    writer = csv.writer(output)

for row in reader:
    print(row)
    coordinates1 = row[1]
    coordinates2 = row[2]

    my_location = gmaps.reverse_geocode(float(coordinates1), 
    float(coordinates2))

    writer.writerow(my_location)

finally:
input.close()
output.close()

I'm getting the error

File "c:/Users/DavidDouglas/code/python/reversegeocode.py", line 13
for row in reader:
  ^
SyntaxError: invalid syntax

This is probably a very silly question that python pros will laugh at, but I'm pretty new to the language and am trying to help out a friend. Thanks for any assitance

1 Answers1

1

Put your for loop inside the try block. Python thinks you're ending your try-except-finally block without an except or finally block when you don't have the for loop inside the try block. I think it's possible to have a try statement with just a finally statement, but it's better to have an except block just in case.

Joel
  • 1,564
  • 7
  • 12
  • 20