![](/img/trans.png)
[英]For python, what does it mean for <class 'requests.models.Response'>
[英]python: Getting error when importing type requests.models.Response into dataframe
我是 python 的新手,我正在嘗試使用人口普查地理編碼服務 API 對地址進行地理編碼,然后將輸出轉換為數據幀。 我已經能夠讀入我的地址文件並且我可以看到輸出,但我似乎無法弄清楚如何將它導入到數據幀中。 我提供了我在下面使用的代碼以及地址文件的內容。
輸出似乎不是 JSON 格式,而是 CSV。 我試圖像導入 CSV 文件一樣導入輸出,但是我無法像導入 CSV 文件一樣弄清楚如何導入變量,而且我無法弄清楚如何將輸出導出到我可以的 CSV 文件進口。
描述 API 的 URL 是https://geocoding.geo.census.gov/geocode...es_API.pdf
import requests
import pandas as pd
import json
url = 'https://geocoding.geo.census.gov/geocoder/geographies/addressbatch'
payload = {'benchmark':'Public_AR_Current','vintage':'Current_Current'}
files = {'addressFile': ('C:\PYTHON_CLASS\CSV\ADDRESS_SAMPLE.csv', open('C:\PYTHON_CLASS\CSV\ADDRESS_SAMPLE.csv', 'rb'), 'text/csv')}
response = requests.post(url, files=files, data = payload)
type(response)
print(response.text)
- 我嘗試了下面的代碼(在許多其他版本中),這是我通常導入 CSV 文件的方式,但它生成一條錯誤消息“文件路徑或緩沖區對象類型無效:<class 'requests.models.Response'>”
df = pd.read_csv(response)
我用來生成地理編碼的地址文件的內容是:
id,地址,城市,州,郵政編碼 1,1600 Pennsylvania Avenue NW, Washington,DC,20500 2,4 S Market St,Boston,MA,02109 3,1200 Getty Center Drive,Los Angeles,CA,90049 4,1800 Congress Ave,Austin,TX,78701 5,One Caesars Palace Drive,Las Vegas,NV,89109 6,1060 West Addison,Chicago,IL,60613 7,One East 161st Street,Bronx,NY,10451 8,201 E Jefferson St,Phoenix, AZ,85004 9,600 N 1st Ave,Minneapolis,MN,55403 10,400 W Church St,Orlando,FL,32801
輸出如下所示:
print(response.text)
"1","1600 賓夕法尼亞大道 NW, 華盛頓特區, 20500","Match","Non_Exact","1600 PENNSYLVANIA AVE NW, WASHINGTON, DC, 20006","-77.03535,38.898754","73","5 L","11","001","006202","1031" "2","4 S Market St, Boston, MA, 02109","Match","Exact","4 S MARKET ST, BOSTON , MA, 02109","-71.05566,42.359936","85723841","R","25","025","030300","2017" "3","1200 Getty Center Drive, Los Angeles, CA , 90049","Match","Exact","1200 GETTY CENTER DR, LOS ANGELES, CA, 90049","-118.47564,34.08857","142816014","L","06","037" 262302","1005" "4","1800 Congress Ave, Austin, TX, 78701","Match","Exact","1800 CONGRESS AVE, AUSTIN, TX, 78701","-97.73847,30.279745" 63946318","L","48","453","000700","1007" "5","One Caesars Palace Drive, Las Vegas, NV, 89109","No_Match" "6","1060 West Addison, Chicago, IL, 60613","Match","Non_Exact","1060 W ADDISON ST, CHICAGO, IL, 60613","-87.65581,41.947227","111863716","R","17" 031","061100","1014","7","One East 161st Street, Bronx, NY, 10451","No_Match""8","201 E Jefferson St, Phoenix, AZ, 85004","Match","Exact","201 E JEFFERSON ST, PHOENIX, AZ, 85004","-112.07113,33.44675","128300920","L","04"," 013","114100","1058" "9","600 N 1st Ave, Minneapolis, MN, 55403","No_Match" "id","address, city, state, zipcode","No_Match" "10" ,"400 W Church St, Orlando, FL, 32801","Match","Exact","400 W CHURCH ST, ORLANDO, FL, 32801","-81.38436,28.540176","94416807","L", “12”、“095”、“010500”、“1002”
response.text
的輸出是:
'"1","1600 賓夕法尼亞大道 NW, 華盛頓特區, 20500","Match","Non_Exact","1600 PENNSYLVANIA AVE NW, WASHINGTON, DC, 20006","-77.03535,38.8987254","8136" "L","11","001","006202","1031"\\n"2","4 S Market St, Boston, MA, 02109","Match","Exact","4 S MARKET ST, BOSTON, MA, 02109","-71.05566,42.359936","85723841","R","25","025","030300","2017"\\n"3","1200 Getty Center Drive , Los Angeles, CA, 90049","Match","Exact","1200 GETTY CENTER DR, LOS ANGELES, CA, 90049","-118.47564,34.08857","142816014","L","06" "037","262302","1005"\\n"4","1800 Congress Ave, Austin, TX, 78701","Match","Exact","1800 CONGRESS AVE, AUSTIN, TX, 78701"," -97.73847,30.279745","63946318","L","48","453","000700","1007"\\n"5","One Caesars Palace Drive, Las Vegas, NV, 89109"," No_Match"\\n"6","1060 West Addison, Chicago, IL, 60613","Match","Non_Exact","1060 W ADDISON ST, CHICAGO, IL, 60613","-87.65581,41.947218","3716 ","R","17","031","061100","1014"\\n"7","One East 161st Street, Bronx, NY, 10451","No_Match"\\n"8","第201章弗森街, 鳳凰城, AZ, 85004","Match","Exact","201 E JEFFERSON ST, PHOENIX, AZ, 85004","-112.07113,33.44675","128300920","L","04" "013","114100","1058"\\n"9","600 N 1st Ave, Minneapolis, MN, 55403","No_Match"\\n"id","address, city, state, zipcode"," No_Match"\\n"10","400 W Church St, Orlando, FL, 32801","Match","Exact","400 W CHURCH ST, ORLANDO, FL, 32801","-81.38436,28.540176"," 94416807","L","12","095","010500","1002"\\n'
當我嘗試
df = pd.read_csv(io.StringIO(response), sep=',', header=None, quoting=csv.QUOTE_ALL)
我收到錯誤信息
TypeError Traceback (most recent call last)
<ipython-input-60-55e6c5ac54af> in <module>
----> 1 df = pd.read_csv(io.StringIO(response), sep=',', header=None, quoting=csv.QUOTE_ALL)
TypeError: initial_value must be str or None, not Response
當我嘗試
df = pd.read_csv(io.StringIO(response.replace('" "', '"\n"')), sep=',', header=None, quoting=csv.QUOTE_ALL)
我有
AttributeError Traceback (most recent call last)
<ipython-input-61-a92a7ffcf170> in <module>
----> 1 df = pd.read_csv(io.StringIO(response.replace('" "', '"\n"')), sep=',', header=None, quoting=csv.QUOTE_ALL)
AttributeError: 'Response' object has no attribute 'replace'
如果response.text
看起來像下面的s
字符串,即有換行符分隔行,您可以嘗試:
>>> import csv
>>> import io
>>> import pandas as pd
>>>
>>> # let's pretend s is response.text
>>> s = '''"1","1600 Pennsylvania Avenue NW, Washington, DC, 20500","Match","Non_Exact","1600 PENNSYLVANIA AVE NW, WASHINGTON, DC, 20006","-77.03535,38.898754","76225813","L","11","001","006202","1031"
... "2","4 S Market St, Boston, MA, 02109","Match","Exact","4 S MARKET ST, BOSTON, MA, 02109","-71.05566,42.359936","85723841","R","25","025","030300","2017"
... "3","1200 Getty Center Drive, Los Angeles, CA, 90049","Match","Exact","1200 GETTY CENTER DR, LOS ANGELES, CA, 90049","-118.47564,34.08857","142816014","L","06","037","262302","1005"
... "4","1800 Congress Ave, Austin, TX, 78701","Match","Exact","1800 CONGRESS AVE, AUSTIN, TX, 78701","-97.73847,30.279745","63946318","L","48","453","000700","1007"'''
>>>
>>> df = pd.read_csv(io.StringIO(s), sep=',', header=None, quoting=csv.QUOTE_ALL)
>>>
>>> with pd.option_context(
... 'display.width', None,
... 'display.max_columns', None,
... 'display.max_colwidth', -1,
... 'display.colheader_justify', 'left'):
... print(df)
...
0 1 2 3 4 \
0 1 1600 Pennsylvania Avenue NW, Washington, DC, 20500 Match Non_Exact 1600 PENNSYLVANIA AVE NW, WASHINGTON, DC, 20006
1 2 4 S Market St, Boston, MA, 02109 Match Exact 4 S MARKET ST, BOSTON, MA, 02109
2 3 1200 Getty Center Drive, Los Angeles, CA, 90049 Match Exact 1200 GETTY CENTER DR, LOS ANGELES, CA, 90049
3 4 1800 Congress Ave, Austin, TX, 78701 Match Exact 1800 CONGRESS AVE, AUSTIN, TX, 78701
5 6 7 8 9 10 11
0 -77.03535,38.898754 76225813 L 11 1 6202 1031
1 -71.05566,42.359936 85723841 R 25 25 30300 2017
2 -118.47564,34.08857 142816014 L 6 37 262302 1005
3 -97.73847,30.279745 63946318 L 48 453 700 1007
如果response.text
沒有分隔行的換行符(但在描述中有空格),則需要將io.StringIO(s)
替換為io.StringIO(s.replace('" "', '"\\n"'))
。
尼古拉斯給出了答案。 我的最終代碼是
import requests
import pandas as pd
import io
import csv
url = 'https://geocoding.geo.census.gov/geocoder/geographies/addressbatch'
payload = {'benchmark':'Public_AR_Current','vintage':'Current_Current'}
files = {'addressFile': ('C:\PYTHON_CLASS\CSV\ADDRESS_SAMPLE.csv', open('C:\PYTHON_CLASS\CSV\ADDRESS_SAMPLE.csv', 'rb'), 'text/csv')}
s = requests.post(url, files=files, data = payload)
df = pd.read_csv(io.StringIO(s.text), sep=',', header=None, quoting=csv.QUOTE_ALL)
with pd.option_context(
'display.width', None,
'display.max_columns', None,
'display.max_colwidth', -1,
'display.colheader_justify', 'left'):
print(df)
謝謝尼古拉斯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.