Mimic Curl In Python
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
Solution 1:
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:
fromStringIO importStringIO
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.
Post a Comment for "Mimic Curl In Python"