python - 如何将 'io.BufferedWriter' 转换为 'Dataframe' - Python

标签 python python-3.x dataframe bufferedwriter

我正在使用以下代码从 One Drive 下载文件:


if response.status_code == 200:
    print('\n> Response Success')

    with open('Book2.xlsx', 'wb') as File:
        File.write(response.content)
        
        print('\n> File Downloaded')
else:
    print('\n> Failed:', response.status_code)
    print(response.content)

代码来自:This post here

"file"来自 One Drive,使用以下代码:


import sys, os, time, requests
import pandas as pd
import urllib.parse

OneDrive_FilePath = 'Book2.xlsx'

OneDrive_FileURL = 'https://graph.microsoft.com/v1.0/me/drive/root:/' + OneDrive_FilePath + ':/content'
OneDrive_FileURL = urllib.parse.quote(OneDrive_FileURL, safe=':/')
print(OneDrive_FileURL)

Client_Id = 'XXXX'
Tenant_Id = 'YYYYY'
Refresh_Token_First = 'ZZZZZ'

PostStr = {'grant_type': 'refresh_token', 'client_id': Client_Id, 'refresh_token': Refresh_Token_First}

Token_Response = requests.post('https://login.microsoftonline.com/' + Tenant_Id + '/oauth2/v2.0/token', data=PostStr)

Access_Token = Token_Response.json()['access_token']
New_Refresh_Token = Token_Response.json()['refresh_token']

if Access_Token is None or New_Refresh_Token is None:
    print('\n> Failed: Access_Token NOT Retrieved')
    sys.exit()

Response = requests.get(OneDrive_FileURL, headers={'Authorization': 'Bearer ' + Access_Token})

正在下载的"file"采用“io.BufferedWriter”的形式。我想实际将"file"作为数据帧加载,以便我可以对其执行某些操作并将其上传到 AWS。

我该怎么做,请帮忙。

谢谢

最佳答案

正如评论中所解释的,File 的类型大多无关紧要。您所需要做的就是直接从响应中读取 Excel,如下所示:

url = "https://go.microsoft.com/fwlink/?LinkID=521962"
res = requests.get(url)
pd.read_excel(res.content)

最后一条语句生成一个常规的 pandas 数据帧。您可以随心所欲地使用它。

关于python - 如何将 'io.BufferedWriter' 转换为 'Dataframe' - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62750822/

相关文章:

python - 在 Python 3 中导入 Pyenchant 时出错

python - 将数据框转换为另一个数据框,将复合字符串单元格拆分为单独的行

Python Pandas 获取行位置名称而不是索引值

python Pandas : Split slash separated strings in two or more columns into multiple rows

python - Apache Spark : Can't use Matplotlib on Jupyter Notebook

python - Sphinx 中尖括号旁边的替换

python - 插入后获取 Tkinter Text 小部件的总显示行数的最有效方法是什么?

python - 如何设置 OpenCV 将原点用作图像的左下角?

python - Pandas - 按整个 MultiIndex 分组

python - 在 Flask 中保护 REST API