我正在使用请求库将 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/