bigpotato
bigpotato

Reputation: 27497

Ruby CSV: How to skip the first two lines of file?

I have a file where the first line is a useless line, and the 2nd is a header. The problem is that when I'm looping through the file, it counts those are rows. Is there a way to use foreach with options to skip 2 lines? I know there's a read method on CSV, but that loads the data into RAM and if the file is too big I don't think it'll scale well.

However, if there is no other option I will consider it. This is what I have so far:

CSV.foreach(filename, col_sep: "\t") do |row|
  until listings.size == limit
    listing_class = 'Sale'
    address = row[7]
    unit = row[8]
    price = row[2]
    url = row[0]
    listings << {listing_class: listing_class, address: address, unit: unit, url: url, price: price}
  end
end

Upvotes: 2

Views: 1474

Answers (3)

engineersmnky
engineersmnky

Reputation: 29308

You can also use #read or #readlines like so

CSV.readlines(filename, col_sep: "\t")[2..-1] do |row|

#readlines is an alias for #read so it does not matter which you use but it splits the CSV into an Array of Arrays so [2..-1] means use rows 3 through the end.

Both this and @Nakilon's answer are probably better and definitely cleaner than using a counter.

As always Ruby classes are well documented and reading the Docs can be much more beneficial than just waiting for someone to hand you an answer.

Upvotes: 1

Nakilon
Nakilon

Reputation: 35054

I didn't benchmark, but try this:

CSV.to_enum(:foreach, filename, col_sep: "\t").drop(2).each do |row|

Upvotes: 5

user4066167
user4066167

Reputation:

Use a counter var, initialize it to 0, and increment it at every line, so if it's smaller than 2 then you can skip to the next row.

Upvotes: 1

Related Questions