python - 如何使用 Python 中的 Paramiko 库在 SFTP 上将 Pandas DataFrame 传输到 .csv?

标签 python pandas sftp paramiko

我想使用 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/

相关文章:

python - 如何在 Django 中渲染单个 View 时将一个模型中的字段用于另一个模型

python - 将属性转换为 Django 模型字段

python - 无法在命令提示符下运行该程序,但可以在 IPython 中运行

python - 递增数组/系列中的连续正组

python - 计算每个箱子中的元素数量

java - 使用私钥身份验证确保 FTP 安全

c# - 使用 WinSCP .NET 程序集连接到 SFTP 服务器时为 "Host key does not match configured key"

python - 如果我不关闭 ssh session 会发生什么?

python - 从一个充满数组的 Python 字典到一个代表所有组合的字典

python - 创建交互变量的函数 : what is wrong with the code?