How to skip the first line of a CSV file and make the second line the header

前端 未结 5 615
有刺的猬
有刺的猬 2020-12-07 02:37

Is there a way to skip the first line of a CSV file and make the second line act as the header?

I have a CSV file that has the date on the first row and the headers

5条回答
  •  失恋的感觉
    2020-12-07 03:31

    I had the same problem (except I wanted to skip more than 1 line at the beginning) and came across this question while looking for a nice solution. For my case, I went with the code described in this answer to a similar question, except that I am also utilizing the headers option as you mentioned you wanted to do.

    CSV.parse(File.readlines(path).drop(1).join, headers: true) do |row|
      # ... now I can use: row['column_name']
    end
    

提交回复
热议问题