I tried searching, but could not find a question about my problem. Let's say I have a CSV file that looks something like this:
Metadata line 1
Metadata line 2
Metadata line 3
Metadata line 4
foo,bar,baz
apple,orange,banana
cashew,almond,walnut
The row foo,bar,bazis the header, and the next row is the corresponding data. When I write my ruby code as follows:
CSV.foreach("filename.csv",:headers=>true) do |row|
puts "#{row}"
end
It breaks clearly. What is the best way to skip the lines before the header? Currently, I think I can do something like:
Find the first row with commas and get line number
Extract that line as an array
Pass that array to :headers
But it seems cumbersome - if I know exactly what the title bar is, what is the best way to go to this line and ignore everything before? Is it possible? If this is a question that was asked before, I will happily devour these answers, maybe my search fu is just not good enough.
Thank you very much!