4

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?

miki725
  • 27,207
  • 17
  • 105
  • 121
Jake Lowen
  • 899
  • 1
  • 11
  • 21
  • Turns out working on the same thing at the same time. Just tried a quick cut and paste with your code and my csv, and it's returning a `status_code` of `200`. Might be a data problem in your CSV- check your data by uploading it here: http://geocoding.geo.census.gov/geocoder/locations/addressbatch?form and see if it works there. – west1737 Aug 03 '15 at 06:10
  • It was always returning a `statuscode` of `200`, but if you inspect the response it still contains "There was an internal error. I've verified the csv is valid via submitting it through the curl example above. I'm thinking I just don't have the request formatted correctly yet to match the curl example. – Jake Lowen Aug 03 '15 at 12:58

3 Answers3

5

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

url = 'http://geocoding.geo.census.gov/geocoder/geographies/addressbatch'
payload = {'benchmark':'Public_AR_Current','vintage':'ACS2013_Current'}
files = {'addressFile': ('Addresses.csv', open('Addresses.csv', 'rb'), 'text/csv')}
r = requests.post(url, files=files, data = payload)
print r.text
Jake Lowen
  • 899
  • 1
  • 11
  • 21
  • Does this still work for you these days? I'm getting SSL handshake errors. If you had any advice, it'd be much appreciated! – vikjam Feb 09 '17 at 18:03
  • @vikjam - Doesn't work for me now. If you have a solution, please share. – shockwave Feb 28 '20 at 16:58
2

May be this is a simpler way to do the same thing.

You will get a clean output in pandas dataframe :)

# pip install censusgeocode    

import censusgeocode
import pandas as pd

cg = censusgeocode.CensusGeocode(benchmark='Public_AR_Census2010',vintage='Census2010_Census2010')
k = cg.addressbatch('D:\WORK\Addresses.csv')

# Bonus
# Get clean output in Dataframe

df = pd.DataFrame(k, columns=k[0].keys())

# PS: I tried with 9990 records in single batch

Reference:

https://pypi.org/project/censusgeocode/

https://geocoding.geo.census.gov/geocoder/benchmarks

https://geocoding.geo.census.gov/geocoder/vintages?form

https://geocoding.geo.census.gov/geocoder/geographies/addressbatch?form

Community
  • 1
  • 1
Suhas_Pote
  • 3,620
  • 1
  • 23
  • 38
0

Works great. Today I just used the code shown below.

url = 'https://geocoding.geo.census.gov/geocoder/locations/addressbatch'
payload = {'benchmark':'Public_AR_Current','vintage':'ACS2013_Current'}
files = {'addressFile': ('19067.csv', open('19067.csv', 'rb'), 'text/csv')}
r = requests.post(url, files=files, data = payload)