pythonpython-requestsgeocodingcensus

Using Census Bulk Geocoder with python requests library


I am experimenting with the census bulk geocode API documentation

The following curl command works:

curl --form addressFile=@Addresses.csv --form benchmark=9 http://geocoding.geo.census.gov/geocoder/locations/addressbatch --output geocoderesult.csv

But when I attempt to port this to python requests:

url = 'http://geocoding.geo.census.gov/geocoder/geographies/addressbatch'
payload = {'benchmark':9}
files = {'addressFile': ('Addresses.csv', open('Addresses.csv', 'rb'), 'text/csv')}
r = requests.post(url, files=files, data = payload)
print r.text

I am apparently not sending a well formed request and only receiving "There was an internal error" in response. Any idea what I am doing wrong in forming this request?


Solution

  • Got it! Turns out that the geographies request type required some parameters that the locations type did not. Working solution:

    url = 'https://geocoding.geo.census.gov/geocoder/geographies/addressbatch'
    payload = {'benchmark':'Public_AR_Current','vintage':'Current_Current', 'format': 'json'}
    files = {'addressFile': ('addresses.csv', open(r'./addresses.csv', 'rb'))}
    r = requests.post(url, files=files, data=payload)