python pandas读取文件内容,读取Python中的FTP文件内容,并将其同时用于Pandas和直接...

I am trying to download a file from an FTP server in memory, transform it to a dataframe but also return it as bytes. Code as follows:

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')

After searching on Stack Overflow, the suggestion was to just read the io stream:

download_file.read()

ValueError: I/O operation on closed file.

Not sure what to try next, without writing the file somewhere and reading it again as bytes.

解决方案

read_csv probably closes the "file". So read it before you call read_csv:

download_file.seek(0)

contents = download_file.read()

download_file.seek(0)

file_to_process = pd.read_csv(download_file, engine='python')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值