[英]How to write Pandas dataframe to Excel file on FTP directly
我想直接從 Pandas Z6A8064B5DF479455550DZ53C77 在 FTP 上創建 Excel 文件我已經嘗試過這段代碼,但我從 Excel 收到“無效的擴展名/損壞的文件”錯誤:
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_excel(buffer)
text = buffer.getvalue()
bio = io.BytesIO(str.encode(text))
ftp.storbinary('STOR filename.xlsx', bio)
Excel 文件是二進制文件。 您永遠不應該嘗試使用StringIO
object 而是直接使用BytesIO
。 此外,無需使用getvalue
制作文件的另一個副本,只需在上傳之前將緩沖區seek
到開頭即可:
buffer = io.BytesIO()
your_pandas_df.to_excel(buffer)
buffer.seek(0)
ftp.storbinary('STOR filename.xlsx', buffer)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.