我想使用 Paramiko 模块将 Python 数据帧作为 .csv 文件直接传输到远程服务器。目前,我将数据帧保存为 .csv,然后将该 .csv 文件推送到服务器。我偶然发现了这个类似的问题How to write pandas dataframe to csv/xls on FTP directly ,但是可以使用Paramiko模块吗?提前致谢!
这是我用来将 .csv 文件从我的目录传输到远程服务器的简单脚本:
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 - 如何使用 Python 中的 Paramiko 库在 SFTP 上将 Pandas DataFrame 传输到 .csv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55248832/