上传 excel 个文件到 sftp 服务器
Upload excel file to sftp server
我有以下代码,它将 csv 格式的 df 上传到 sftp 服务器。
with sftp.open(mypath + timestr + '.csv', "w") as f:
f.write(df_pdcs_ec.to_csv(index=False))
但是:有没有办法以 excel 格式上传相同的 df?
更新:我正在尝试根据@furas 的回答生成带有 2 个选项卡的文件,但只得到一个。
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
中,所有功能都应与具有功能 write()
的 file handlers
或 file-like objects
一起使用,因此您可以打开文件或连接并使用 to_csv(file_handler)
to_excel(file_handler)
等
但对于 excel
你必须以字节模式写入它 - wb
with sftp.open(mypath + timestr + '.csv', "wb") as f:
df_pdcs_ec.to_csv(f, index=False)
with sftp.open(mypath + timestr + '.xlsx', "wb") as f:
df_pdcs_ec.to_excel(f, index=False)
with sftp.open(mypath + timestr + '.html', "wb") as f:
df_pdcs_ec.to_html(f, index=False)
编辑:
要保存许多选项卡,您必须使用 ExcelWriter
(请参阅文档:to_excel)
我无法使用 sftp.open()
对其进行测试,但这应该可以。至少它适用于标准 open()
.
with sftp.open(mypath + timestr + '.xlsx', "wb") as f:
with pd.ExcelWriter(f) as writer:
df_pdcs_ec.to_excel(writer, sheet_name='Test1', index=False)
df_pdcs_ec.to_excel(writer, sheet_name='Test2', index=False)
我有以下代码,它将 csv 格式的 df 上传到 sftp 服务器。
with sftp.open(mypath + timestr + '.csv', "w") as f:
f.write(df_pdcs_ec.to_csv(index=False))
但是:有没有办法以 excel 格式上传相同的 df?
更新:我正在尝试根据@furas 的回答生成带有 2 个选项卡的文件,但只得到一个。
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
中,所有功能都应与具有功能 write()
的 file handlers
或 file-like objects
一起使用,因此您可以打开文件或连接并使用 to_csv(file_handler)
to_excel(file_handler)
等
但对于 excel
你必须以字节模式写入它 - wb
with sftp.open(mypath + timestr + '.csv', "wb") as f:
df_pdcs_ec.to_csv(f, index=False)
with sftp.open(mypath + timestr + '.xlsx', "wb") as f:
df_pdcs_ec.to_excel(f, index=False)
with sftp.open(mypath + timestr + '.html', "wb") as f:
df_pdcs_ec.to_html(f, index=False)
编辑:
要保存许多选项卡,您必须使用 ExcelWriter
(请参阅文档:to_excel)
我无法使用 sftp.open()
对其进行测试,但这应该可以。至少它适用于标准 open()
.
with sftp.open(mypath + timestr + '.xlsx', "wb") as f:
with pd.ExcelWriter(f) as writer:
df_pdcs_ec.to_excel(writer, sheet_name='Test1', index=False)
df_pdcs_ec.to_excel(writer, sheet_name='Test2', index=False)