- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想读取安全 SFTP 文件夹上的一些 CSV/Excel 文件,在这些文件中进行一些更改(每个文件中的固定更改,如删除第 2 列),将它们上传到 Postgre 数据库,并将它们上传到不同的数据库Python 中的 SFTP 路径
最好的方法是什么?
我已使用 pysftp 库连接到 SFTP,并正在读取 Excel:
import pysftp
import pandas as pd
myHostname = "*****"
myUsername = "****"
myPassword = "***8"
cnopts =pysftp.CnOpts()
cnopts.hostkeys = None
sftp=pysftp.Connection(host=myHostname, username=myUsername,
password=myPassword,cnopts=cnopts)
print ("Connection succesfully stablished ... ")
sftp.chdir('test/test')
#sftp.pwd
a=[]
for i in sftp.listdir_attr():
with sftp.open(i.filename) as f:
df=pd.read_csv(f)
我应该如何继续上传到数据库并对 CSV 进行永久更改?
最佳答案
您已完成下载部分。
上传部分参见How to Transfer Pandas DataFrame to .csv on SFTP using Paramiko Library in Python? – 虽然是给Paramiko的,pysftp Connection.open
method行为与 Paramiko SFTPClient.open
相同,所以代码是相同的(不过,你 should not use pysftp )。
完整代码可以是这样的:
with sftp.open("/remote/path/data.csv", "r+", bufsize=32768) as f:
# Download CSV contents from SFTP to memory
df = pd.read_csv(f)
# Modify as you need (just an example)
df.at[0, 'Name'] = 'changed'
# Upload the in-memory data back to SFTP
f.seek(0)
df.to_csv(f, index=False)
# Truncate the remote file in case the new version of the contents is smaller
f.truncate(f.tell())
上面更新了相同的文件。如果您想上传到不同的文件,请使用:
# Download CSV contents from SFTP to memory
with sftp.open("/remote/path/source.csv", "r") as f:
df = pd.read_csv(f)
# Modify as you need (just an example)
df.at[0, 'Name'] = 'changed'
# Upload the in-memory data back to SFTP
with sftp.open("/remote/path/target.csv", "w", bufsize=32768) as f:
df.to_csv(f, index=False)
有关 bufsize
的用途,请参阅:
Writing to a file on SFTP server opened using Paramiko/pysftp "open" method is slow
强制警告:请勿设置cnopts.hostkeys = None
,除非您不关心安全性。正确的解决方案请参阅 Verify host key with pysftp 。
关于python - 从 SFTP 文件读取 CSV/Excel 文件,使用 Pandas 对这些文件进行一些更改,然后保存回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61817226/
如果您登录到 sftp 服务器, 用户名和密码是否安全发送? 或者您是否必须进行基于证书的身份验证以确保整个传输都是加密的? 如果这是依赖于客户端的,那么您知道 Tumbleweed 和 WinSCP
我们如何在不安装任何软件/工具的情况下验证服务器上是否已授予 SFTP 访问权限? 最佳答案 大多数服务器有 curl和 scp安装,您可以使用它登录到 SFTP 服务器。要使用 curl 测试您的凭
我需要为 SFTP 执行校验和(为了数据完整性)。我希望这可以在 SFTP 文件传输期间完成 - 我意识到这可能取决于产品(仅供引用:使用 CLEO VLTrader),但想知道这是否是惯例? 我也在
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 8年前关闭。 Improve this que
我目前有一个 SFTP 脚本,它通过每天在 cron 上运行的 SSH 将最新版本的文件上传到服务器。但是我总是收到这样的电子邮件(我将服务器更改为 example.com fyi) /etc/cro
是否有使用“sftp”将远程服务器上的远程文件移动到另一个目录(在同一远程服务器上)的单行程序? 最佳答案 对于sftp,它是rename。如果你要求调用 sftp,使用 bash 就像是 sftp
嗨,我需要使用 NetSuite sFtp 功能从保存的搜索和 ftp'it 创建一个文件到另一台服务器。我想知道返回的数据后如何创建文件并将其传递给 FTP 对象?它将是一种csv形式,我可以将
我在 SFTP 位置创建了一个目录,我想将文件从一个 SFTP 目录移动到另一个目录,但是 cp那里不支持命令。 我怎样才能做到这一点? 最佳答案 核心 SFTP 协议(protocol)不支持复制远
我正在努力解决的问题有点复杂。我先向您解释设置环境。 我正在使用 PHPStorm 来处理 Symony2 项目。 我的 Apache 托管在通过“部署工具”连接到 PHPStorm 的 Debian
使用FTP,一切正常...但是.. 当我使用 SFTP 时,它成功连接,甚至当我使用“上传按钮”时,它成功上传文件...但是当我编辑+保存文件,它开始自动上传更改时,它无法上传(红色消息:文件....
我想将文件从远程服务器复制到不同目录中。例如,我想一次运行这4个命令。 scp remote:A/1.txt local:A/1.txt scp remote:A/2.txt local:A/2.tx
我可以使用 ssh/sftp(不带 -b 选项)登录我的 Remote sftp root@192.168.7.2 但是当我尝试 sftp -b commands.tmp root@192.168.7
我正在使用 Airflow v1.9rc5 中的 sftp_operator。我试图通过 SFTP 获取两个文件,但我得到: 错误 - 无法连接到主机:192.168.56.101,错误:没有可用的身
我想将 .png 文件从我计算机上的目录传输到远程服务器上的目录。 我必须使用 SFTP 来保护文件和传输模式。而且我已经有一个 UNIX 脚本 (.ksh) 文件以正常模式复制文件。如何在 SFTP
我正在使用 JSch 作为 SFTP 客户端,现在由于某些安全限制,我需要启用被动模式。 但我找不到启用被动模式的方法。 有人可以告诉我该怎么做吗? 最佳答案 “被动模式”是 FTP 协议(proto
我在 DigitalOcean 上有一个 Droplet,并创建了 SSH key 。我的 SSH 和 SFTP 访问与 Mac OSX 终端和传输完美配合。 但由于某种原因,SFTP 无法与 PHP
最近,我正在使用 SharpSSH 来处理远程文件。 SharpSSH 提供了用于删除远程目录的 rmdir API,但我发现当目录不为空时它会抛出异常并显示消息“Permission denied”
尝试通过 SFTP 连接到另一个集群中的文件,但没有任何效果。 Current spark version is : '2.2.0.2.6.4.0-91 . scala :2.11.8' . 下面是数
我已经使用 Package Installer (control+shift+p) 安装了 SFTP,但是现在当我点击“添加新服务器”时,无论我是哪个文件,它都只是添加了代码/json,我必须输入我的
我使用 DSL 配置和 spring。 我的路线如下: @Component public class UploadRoutesDefinition extends RouteBuilder {
我是一名优秀的程序员,十分优秀!