Alb*_*osa 4 python sftp paramiko pandas
我想使用 Paramiko 模块将 Python 数据帧作为 .csv 文件直接传输到远程服务器。目前,我将数据帧另存为 .csv,然后将该 .csv 文件推送到服务器。我偶然发现了这个类似的问题How to write pandas dataframe to csv/xls on FTP direct ,但是否可以使用 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')
Run Code Online (Sandbox Code Playgroud)
只需使用 SFTPClient.open
with sftp.open('path_to_remote_file', "w") as f:
f.write(df.to_csv(index=False))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4416 次 |
| 最近记录: |