4

I have a rake file that pulls in data from an external CSV file and enumerates through it with:

CSV.foreach(file, :headers => true) do |row|

What is the most effective way (in ruby) to specify the starting point within the spreadsheet?

:headers => true allows me to start importing from the second line, but what if I want to start a line 20?

dmt2989
  • 1,610
  • 3
  • 17
  • 30

2 Answers2

2

Ruby enumerators include a drop method that will skip over the first n items.

When not passed a block, CSV.foreach returns an enumerator.

You can use

CSV.foreach(file, :headers => true).drop(20).each do |row|

This will skip the first 20 data rows (the header row does NOT count as one of those twenty).

Lucifer
  • 29,392
  • 25
  • 90
  • 143
akatakritos
  • 9,836
  • 1
  • 23
  • 29
  • 1
    the only thing is it still load all file in memory. anyway, good solution. – Paritosh Piplewar Apr 06 '14 at 19:55
  • Yeah just noticed that. Can't think of a good reason it would return an array instead of another enumerator – akatakritos Apr 06 '14 at 19:56
  • 1
    See also: http://stackoverflow.com/questions/2200014/ruby-is-there-something-like-enumerabledrop-that-returns-an-enumerator-instead – akatakritos Apr 06 '14 at 20:02
  • Thanks for the help. I was able to get it to work with CSV.open(file, :headers => true).drop(20).each do |row|. Please edit your answer to show ".open" instead of ".foreach" and I will select it as correct. Thx again. – dmt2989 Apr 06 '14 at 23:59
  • Oops, I inadvertedly downvoted your answer. Didn't mean to. Can't undo that until you modify the question! I will be watching for it and fix it. – Redoman May 26 '14 at 05:00
1

Use .drop(#rows to ignore):

CSV.open(file, :headers => true).drop(20).each do |row|

dmt2989
  • 1,610
  • 3
  • 17
  • 30