How to skip the first three lines instead of the first in FasterCSV

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

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

but the problem is that my csv has the first 3 lines as headers ... any way to make fastCSV skip the first three lines, not just the first

+5
source share
3 answers

Not sure about FasterCSV, but in the Ruby 1.9 CSV standard 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
+19
source

I am not a FasterCSV user, but why not do the control myself:

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
+5

. ... - 1.9, , , POS. ,

c = CSV.open iFileName
logger.debug c.first
logger.debug c.first
logger.debug c.first

In your journal you will get three different results. One for each of the three header lines.
c.each do |row|# now it seems to start on the 4th line.

It makes sense that he will read the file this way. Then he should have only the current line in memory.

I still like the answer Mladen Yablanovich, but this is also interesting logic.

0
source

All Articles