3

The following curl code works:

curl --form [email protected] --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)
2
  • Can you show your code? Commented Jul 29, 2014 at 20:12
  • 1
    I followed up to chrisaycock 's comment by posting code. @MarcB , your comment isn't even close to be constructive. Commented Jul 29, 2014 at 20:25

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.