Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i skip over the first three rows instead of the only the first in FasterCSV

I am using FasterCSV and i am looping with a foreach like this

FasterCSV.foreach("#{Rails.public_path}/uploads/transfer.csv", :encoding => 'u', :headers => :first_row) do |row|

but the problem is my csv has the first 3 lines as the headers...any way to make fasterCSV skip the first three rows rather then only the first??

like image 446
Matt Elhotiby Avatar asked Sep 07 '11 15:09

Matt Elhotiby


2 Answers

Not sure about FasterCSV, but in Ruby 1.9 standard CSV library (which is made from FasterCSV), I can do something like:

c = CSV.open '/path/to/my.csv'
c.drop(3).each do |row|
  # do whatever with row
end
like image 165
Mladen Jablanović Avatar answered Nov 16 '22 00:11

Mladen Jablanović


I'm not a user of FasterCSV, but why not do the control yourself:

additional_rows_to_skip = 2
FasterCSV.foreach("...", :encoding => 'u', :headers => :first_row) do |row|
    if additional_rows_to_skip > 0
        additional_rows_to_skip -= 1
    else
        # do stuff...
    end
end
like image 5
Joaquim Rendeiro Avatar answered Nov 16 '22 00:11

Joaquim Rendeiro