我想将.xlsx文件从FTP连接中读取为熊猫数据,但是我想在内存中这样做,而不将.xlsx写入本地磁盘。
以下是我的当前代码:
filename = 'my_excel.xlsx'
localfile = open(filename, 'wb')
ftp.retrbinary('RETR ' + filename, localfile.write, 1024)
ftp.quit()
localfile.close()在本节之后,我可以将本地磁盘中的.xlsx读取为熊猫数据格式,但是我想在缓冲区内存中处理这个问题。
我该怎么做呢。谢谢你的help=)
发布于 2021-11-04 22:32:51
您可以使用io.BytesIO对象将数据流写入其中,然后将其读入熊猫。
import pandas as pd
from io import BytesIO
excel_fp = BytesIO()
filename = 'my_excel.xlsx'
ftp.retrbinary('RETR ' + filename, excel_fp.write, 1024)
# reset the cursor to the start of the file stream
excel_fp.seek(0)
df = pd.read_excel(excel_fp)https://stackoverflow.com/questions/69846509
复制相似问题