1

I am trying to geocode a list of addresses in a CSV file. The column with the information is named "full" and it looks like this:

full
100 Ross St,15219
1014 Blackadore Ave.,15208
1026 Winterton St.,15206
...

This is the code that I am using:

import csv
import pygeocoder
import pandas as pd

from pygeocoder import Geocoder

df = pd.read_csv('C:\Users\Jesus\Desktop\Events.csv')
address = df.full

result = Geocoder.geocode(address)
print(result[0].coordinates)

And this is the output:

Traceback (most recent call last):
  File "C:\Users\Jesus\Desktop\python\geocode.py", line 10, in <module>
    result = Geocoder.geocode(address)
  File "C:\Python27\lib\site-packages\pygeocoder.py", line 160, in geocode
    return GeocoderResult(Geocoder.get_data(params=params))
  File "C:\Python27\lib\site-packages\pygeocoder.py", line 107, in get_data
    response_json = response.json()
  File "C:\Python27\lib\site-packages\requests\models.py", line 693, in json
    return json.loads(self.text, **kwargs)
  File "C:\Python27\lib\site-packages\simplejson\__init__.py", line 488, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\site-packages\simplejson\decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "C:\Python27\lib\site-packages\simplejson\decoder.py", line 389, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
  File "C:\Python27\lib\site-packages\simplejson\scanner.py", line 122, in scan_once
    return _scan_once(string, idx)
  File "C:\Python27\lib\site-packages\simplejson\scanner.py", line 118, in _scan_once
    raise JSONDecodeError(errmsg, string, idx)
JSONDecodeError: Expecting value: line 1 column 1 (char 0
David Cain
  • 16,484
  • 14
  • 65
  • 75
asado23
  • 366
  • 1
  • 7
  • 20

1 Answers1

2

Your address variable is a Series object from pandas, which may be causing this problem. To geocode all the addresses from the CSV, iterate through it like:

for a in address:
    result = Geocoder.geocode(a)
    print(result[0].coordinates)

To store the results in a file (assuming Python 2.x):

with open('filename', 'w') as outfile:
    for a in address:
        result = Geocoder.geocode(a)
        print >>outfile, str(result[0].coordinates) # Prints to file

You can do outfile.write( str(result[0].coordinates) ) instead of the print if you want. The only difference is that print automatically adds a line break. To add to a list, just declare the list outside of your for statement (such as coordinates_list = []) and replace the print with coordinates_list.append(result[0].coordinates). Either of these ways will work in Python 3.x, but the print >>outfile statement will not.

Goluxas
  • 177
  • 3
  • 8