Search code examples
pythoncsvgeopy

Too many values to unpack with python CSV reader and Geopy


import csv
from geopy import geocoders

g = geocoders.Google()

spamReader = csv.reader(open('locations.csv', 'rb'), delimiter='\t', quotechar='|')

for row in spamReader:
    a = ', '.join(row)
    #print a
    place, (lat, lng) = g.geocode(a, exactly_one=False)
    print "%s: %.5f, %.5f" % (place, lat, lng)

The data inside locations.csv looks like:

6943     Australia
6944     Australia
6945     Australia
6946     Australia
6947     Australia
6951     Australia

For some reason I am left with a "too many values to unpack" error. The values do print out if I use the commented print statement. Does anyone know why this would be happening?


Solution

  • The problem is the exactly_one argument to g.geocode. When I run this in the shell I get:

     >>> g.geocode('6943, Australia', exactly_one=False)
     [(u'Australia 6943, Villafontana, Tijuana Municipality, Baja California, Mexico',
      (32.4988788, -116.8620506)),
     (u'Australia 6943, Castelar, Buenos Aires Province, Argentina',
      (-34.7036339, -58.6423071)),
     (u'Australia 6943, Rosario, Santa Fe Province, Argentina',
      (-32.9913482, -60.6398934)),
     (u'Australia, Lebanon', (33.8879118, 35.4749439)),
     (u'Australia, Juliaca, Peru', (-15.4897806, -70.146677)),
     (u'Australia, Lima District 15007, Peru', (-12.0397296, -76.9944836)),
     (u'Australia, Manila, Philippines', (14.48538, 121.0394822)),
     (u'Australia, Conchal\xed, Santiago Metropolitan Region, Chile',
      (-33.3929606, -70.6780826)),
     (u'Australia, Chiguayante, Biob\xedo Region, Chile',
      (-36.9556346, -73.0145556)),
     (u'Australia, Copiap\xf3, Atacama Region, Chile', (-27.3978776, -70.2934656))]
    

    Now, you're trying to break up that big list into just place, (lat, lng), when it's actually a list of those; there are too many values in that list to unpack into just the two (place and (lat, lng)), since there are actually 10. You could do something like

    for place, (lat, lng) in g.geocode(a, exactly_one=False):
         print place, lat, lng
    

    or do some other kind of list manipulation or whatever.