我正在尝试从内存中的FTP服务器下载文件,将其转换为数据帧,但也以字节为单位返回。代码如下:
import io
import pandas as pd
from ftplib import FTP
ftp_connection.cwd(ftp_folder)
download_file = io.BytesIO()
ftp_connection.retrbinary('RETR ' + str(file_name), download_file.write)
download_file.seek(0)
file_to_process = pd.read_csv(download_file, engine='python')
搜索堆栈溢出后,建议只是读取io流:
download_file.read()
ValueError: I/O operation on closed file.
不知道接下来要尝试什么,而无需将文件写入某处并以字节为单位再次读取。
read_csv
可能会关闭“文件”。因此,请在致电read_csv
之前先阅读它:
download_file.seek(0)
contents = download_file.read()
download_file.seek(0)
file_to_process = pd.read_csv(download_file, engine='python')