Question

The following curl code works:

curl --form addressFile=@t.csv --form benchmark=Public_AR_Census2010 http://geocoding.geo.census.gov/geocoder/locations/addressbatch

t.csv is simply

1, 800 Wilshire Blvd, Los Angeles, CA, 90017

How do I mimic this in python. So far all of my attempts have resulted in 'Bad Requests'. I am also trying to keep everything in memory--no writing to file.

One attempt:

import requests
url = "http://geocoding.geo.census.gov/geocoder/json/addressbatch"

# data is csv like string with , and \n
ddata = urllib.urlencode({'addressFile' : data, 'benchmark' : 'Public_AR_Current'})
r = requests.get(url + "?" + ddata) # Forcibly closed by remote

requests.put("http://geocoding.geo.census.gov/geocoder/json/addressbatch", ddata)
Was it helpful?

Solution

One option would be to use requests:

import requests

url = "http://geocoding.geo.census.gov/geocoder/locations/addressbatch"
data = {'benchmark': 'Public_AR_Census2010'}
files = {'addressFile': open('t.csv')}

response = requests.post(url, data=data, files=files)
print response.content

prints:

"1"," 800 Wilshire Blvd,  Los Angeles,  CA,  90017","Match","Exact","800 Wilshire Blvd, LOS ANGELES, CA, 90017","-118.25818,34.049366","141617176","L"

In case you need to handle the csv data in memory, initialize a StringIO buffer:

from StringIO import StringIO
import requests

csv_data = "1, 800 Wilshire Blvd, Los Angeles, CA, 90017"
buffer = StringIO()
buffer.write(csv_data)
buffer.seek(0)

url = "http://geocoding.geo.census.gov/geocoder/locations/addressbatch"
data = {'benchmark': 'Public_AR_Census2010'}
files = {'addressFile': buffer}

response = requests.post(url, data=data, files=files)
print response.content

This prints the same result as is with using a real file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top