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

Python: How to read excel file from Requests response?

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

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

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

您可以直接使用 pandas 中的 url 来读取 excel 文件,而无需使用请求。

import pandas as pd

df = pd.read_excel(my_url)

如果需要通过请求检索数据,那么这里 () 的回答可能就足够了:

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)