如何使用Python中的Paramiko库在SFTP上将Pandas DataFrame传输到.csv? [英] How to Transfer Pandas DataFrame to .csv on SFTP using Paramiko Library in Python?

查看:262
本文介绍了如何使用Python中的Paramiko库在SFTP上将Pandas DataFrame传输到.csv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Paramiko模块将Python数据帧作为.csv文件直接传输到远程服务器.当前,我将数据帧另存为.csv,然后将那个.csv文件推送到服务器.我偶然发现了类似的问题如何写熊猫数据帧直接传输到FTP上的csv/xls ,但是可以使用Paramiko模块吗?预先感谢!

I want to transfer a Python dataframe directly as a .csv file to a remote server using Paramiko module. Currently, I save the dataframe as a .csv then I push that .csv file to the server. I stumbled by this similar question How to write pandas dataframe to csv/xls on FTP directly, but is it possible using Paramiko module? Thanks in advance!

这是我用来将.csv文件从目录传输到远程服务器的简单脚本:

This is the simple script I use to transport a .csv file from my directory to the remote server:

import pandas as pd
import paramiko

# Save DataFrame as CSV
file_name = 'file.csv'
df.to_csv(file_name,index=False)

# Connect to Server Via FTP
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='host',username='user_name',password='password')
ftp_client= ssh_client.open_sftp()

# Upload 'file.csv' to Remote Server
ftp_client.put('path_to_file.csv','path_to_remote_file')

推荐答案

只需使用 SFTPClient.open

with sftp.open('path_to_remote_file', "w") as f:
    f.write(df.to_csv(index=False))

这篇关于如何使用Python中的Paramiko库在SFTP上将Pandas DataFrame传输到.csv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆