ruby net-sftp read file line by line

假如想象 提交于 2019-12-10 17:11:24

问题


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 

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?


回答1:


I solved this by doing this:

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

data.each do |line|
  puts line
end



回答2:


The proper answer for this would actually be to use the file.eof? value.

The code would look like:

require 'net/sftp'
sftp = Net::SFTP.start('ftp.app.com','username', :password => 'password')
sftp.file.open("/path/to/remote/file.csv", "r") do |f|
  while !f.eof?
    puts f.gets
  end
end

Documentation can be found here




回答3:


In my case something like this worked:

data = sftp.download!("/path/to/remote/file.csv").split(/\n/).map{ |e| e.split(/,/).map{ |x| x.gsub(/"/, "")} }

data.each do |line|
  puts line
end

Will also split each row of the .csv into different array columns and remove any excess of "". Note this is for mac where line breaks are \n.



来源:https://stackoverflow.com/questions/22256127/ruby-net-sftp-read-file-line-by-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!