5

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?

Luigi
  • 5,443
  • 15
  • 54
  • 108

3 Answers3

5

I solved this by doing this:

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

data.each do |line|
  puts line
end
Luigi
  • 5,443
  • 15
  • 54
  • 108
  • 2
    Your solution doesn't seem to meet your requirement "without downloading the file?" – user229044 Mar 07 '14 at 19:23
  • I edited my question to add "locally". This method just downloads the file into memory, so there is no physical copy of the file that I have to store. – Luigi Mar 07 '14 at 19:34
3

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

dibi
  • 3,257
  • 4
  • 24
  • 31
nubecoder
  • 31
  • 2
0

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.

juan Isaza
  • 3,646
  • 3
  • 31
  • 37