gpt4 book ai didi

python - Pandas xlrd 引擎传递了静态值错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:56 25 4
gpt4 key购买 nike

我正在尝试从 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')

关于python - Pandas xlrd 引擎传递了静态值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50307145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com