将人口普查批量地理编码器与 python 请求库一起使用
Using Census Bulk Geocoder with python requests library
我正在试验人口普查批量地理编码 API documentation
以下 curl 命令有效:
curl --form addressFile=@Addresses.csv --form benchmark=9 http://geocoding.geo.census.gov/geocoder/locations/addressbatch --output geocoderesult.csv
但是当我尝试将其移植到 python 请求时:
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
我显然没有发送格式正确的请求,只收到 "There was an internal error" 响应。知道我在形成此请求时做错了什么吗?
知道了!事实证明,地理请求类型需要一些位置类型不需要的参数。工作解决方案:
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
效果很好。今天我只使用了下面显示的代码。
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)
可能这是做同样事情的更简单的方法。
您将在 pandas 数据帧中获得干净的输出:)
# 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
参考:
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
我正在试验人口普查批量地理编码 API documentation
以下 curl 命令有效:
curl --form addressFile=@Addresses.csv --form benchmark=9 http://geocoding.geo.census.gov/geocoder/locations/addressbatch --output geocoderesult.csv
但是当我尝试将其移植到 python 请求时:
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
我显然没有发送格式正确的请求,只收到 "There was an internal error" 响应。知道我在形成此请求时做错了什么吗?
知道了!事实证明,地理请求类型需要一些位置类型不需要的参数。工作解决方案:
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
效果很好。今天我只使用了下面显示的代码。
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)
可能这是做同样事情的更简单的方法。
您将在 pandas 数据帧中获得干净的输出:)
# 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
参考:
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