Python:如何从请求响应中读取 excel 文件?

标签 python pandas python-requests xlrd

我正在使用请求库将 excel 文件下载为流。

r = requests.get(my_url, stream=True)

我想读取这个 excel 文件中的数据,为此我可以尝试使用 pandas。但我不确定如何从收到的响应中读取文件。我能做些什么?

最佳答案

你可以直接在pandas中使用一个url来读取excel文件,而不需要使用requests。

import pandas as pd

df = pd.read_excel(my_url)

如果需要通过请求检索数据,那么这里的答案 ( How to download a Excel file from behind a paywall into a pandas dataframe? ) 可能就足够了:

Simply wrap the file contents in a BytesIO:

import pandas as pd
import io

with io.BytesIO(r.content) as fh:
    df = pd.io.excel.read_excel(fh, sheetname=0)

关于Python:如何从请求响应中读取 excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58147689/

相关文章:

python - 如何根据 google chrome 检查器提供的信息发出 xml http post 请求

python - 在 Amazon EC2 上安装 numpy

python - 运行时变量创建 Python 3.1.2

python - 基于Python中的逻辑表达式使用for循环创建新列

python - Pandas 获得连续的行

Python "responses"模拟 - 无法多次调用一个 URL

python - Django 保存科学数字

python - 使用 pythonbrew 编译 Python 3.2 和 2.7 时出现问题

python - 正在解决试图在 DataFrame 的切片副本上设置值

javascript - 在 Python 中使用回调下载 CSV