我有下面的代码,它将csv格式的df上传到sftp服务器
with sftp.open(mypath + timestr + '.csv', "w") as f:
f.write(df_pdcs_ec.to_csv(index=False))
但是:有没有办法以excel格式上传相同的df
更新:我正试图根据@furas的回答生成包含两个选项卡的文件,但只得到一个选项卡
with sftp.open(mypath + timestr + '.xlsx', "wb") as f:
df_pdcs_ec.to_excel(f, sheet_name = 'Test1', index=False)
df_pdcs_ec.to_excel(f, sheet_name = 'Test2', index=False)
我不能测试它,但在}等
Pandas
中,所有函数都应该与file handlers
或file-like objects
一起工作,它们具有函数write()
,因此您可以打开文件或连接并使用to_csv(file_handler)
{但是对于
excel
您必须以字节模式写入它-wb
编辑:
要保存多个选项卡,必须使用
ExcelWriter
(请参见文档:to_excel)我不能用
sftp.open()
测试它,但这应该可以。至少它可以与标准open()
一起工作相关问题 更多 >
编程相关推荐