ruby net-sftp 逐行读取文件 [英] ruby net-sftp read file line by line

查看:58
本文介绍了ruby net-sftp 逐行读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ruby​​ 2.0.0 和 rails 4.0.0.我有类似的东西:

I am using ruby 2.0.0 and rails 4.0.0. I have something similar to this:

require 'net/sftp'


 sftp = Net::SFTP.start('ftp.app.com','username', :password => 'password')

 sftp.file.open("/path/to/remote/file.csv", "r") do |f|
    puts f.gets
 end 

这会在 FTP 站点上打开文件,但它只放置 csv 文件的第一行.我需要逐行读取这个文件,最好忽略标题.

This opens the file on the FTP site, but it only puts the first line of the csv file. I need to read this file row by row, preferably ignoring the header.

如何逐行读取文件,而无需在本地下载文件?

How can I read the file row by row, without downloading the file locally?

推荐答案

我通过这样做解决了这个问题:

I solved this by doing this:

data = sftp.download!("/path/to/remote/file.csv").split(/\r\n/)

data.each do |line|
  puts line
end

这篇关于ruby net-sftp 逐行读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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