[英]Pandas xlrd engine passed still value error
我試圖從URL中讀取xls文件:
使用請求:
page = requests.get(url) # xls url
df = pd.read_excel(page.content,engine = 'xlrd') #engine is passed
File "f:\python36\lib\site-packages\pandas\util\_decorators.py", line 118, in wrapper
return func(*args, **kwargs)
File "f:\python36\lib\site-packages\pandas\io\excel.py", line 230, in read_excel
io = ExcelFile(io, engine=engine)
File "f:\python36\lib\site-packages\pandas\io\excel.py", line 296, in __init__
raise ValueError('Must explicitly set engine if not passing in'
ValueError: Must explicitly set engine if not passing in buffer or path for io.
# if i put in random engine name it throws an unsupported engine error but with xlrd it throws must set engine
我嘗試保存文件,然后閱讀它:
with open('file.xls','wb') as f:
f.write(page.content)
df = pd.read_excel('file.xls',engine='xlrd') #this works
編輯:
我試過傳遞page.text它引發:
ValueError: embedded null character
如果pd.read_excel
的第一個參數是str
,則將其解釋為文件(或URL)的路徑 。 如果我們希望將文件的內容直接傳遞給read_excel
,那么我們需要將內容包裝在BytesIO
,使其成為類文件對象:
因此,使用
BytesIO = pd.io.common.BytesIO
df = pd.read_excel(BytesIO(page.content), engine='xlrd')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.