0

New to python, I have a cvs file with lat/long and I am trying to retrieve the addresses, how can I pass my lat/long string to geolocator

with open('try.csv', 'rb') as f:
   reader = csv.DictReader(f)
   for row in reader:
    geolat = row['pickup_latitude']
    geolong = row['pickup_longitude']
    # point = Point(float(row['pickup_latitude']),     float(row['pickup_longitude']))

    val1 = ("\""+geolat+", ")
    val2 = (geolong+"\"")
    val = val1 + val2
    print val
# var returns "40.750912, -73.99102"
location = geolocator.reverse(val)
lmundia
  • 11
  • 1

1 Answers1

1

geolocator.reverse accepts a single comma-separated string as its argument.

with open('try.csv', 'rb') as f:
    reader = csv.DictReader(f)
    for row in reader:
        geolat = row['pickup_latitude']     # geolat is a string
        geolong = row['pickup_longitude']   # geolong is a string
        latlong = [geolat, geolong]         # latlong is a list of two strings
        val = ", ".join(latlong)            # val is one comma-separated string

        location = geolocator.reverse(val)
        print(location)

Of course you can cut out all the intermediate steps at the expense of readability.

with open('try.csv', 'rb') as f:
    reader = csv.DictReader(f)
    for row in reader:
        location = geolocator.reverse(", ".join([row['pickup_latitude'], row['pickup_longitude']]))
        print(location)

Rembember, the line indent convention in Python is 4 spaces.

Tomalak
  • 332,285
  • 67
  • 532
  • 628