如何直接从 Pandas 数据帧在 ftp 上创建 .csv 或 .xls 文件,因为我不想在本地计算机上创建一个文件,然后将其推送到 ftp。非常感谢:)
最佳答案
作为the docs说:
Store a file in binary transfer mode. cmd should be an appropriate
STOR
command:"STOR filename"
. file is a file object (opened in binary mode) which is read until EOF using itsread()
method in blocks of size blocksize to provide the data to be stored…
因此,您需要为其提供一个具有适当的read
方法的类文件对象。
字符串不是类文件对象,而是一个 io.BytesIO
是。所以:
from ftplib import *
from StringIO import StringIO
import pandas
import io
ftp = FTP('ftp.mysite.com')
ftp.login('un', 'pw')
ftp.cwd('/')
buffer = StringIO.StringIO()
your_pandas_df.to_csv(buffer)
text = buffer.getvalue()
bio = io.BytesIO(str.encode(text))
ftp.storbinary('STOR filename.csv', bio)
关于python - 如何直接在FTP上将pandas dataframe写入csv/xls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46071686/