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

前端 未结 5 594
有刺的猬
有刺的猬 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:11

    You can do this

    text = File.readlines("file.csv")[1..-1].join()
    csv = CSV.parse(text, headers: true)
    
    0 讨论(0)
  • 2020-12-07 03:24

    Depending on your data you may use another approach with theskip_lines-option

    This examples skip all lines with a leading #

    require 'csv'
    
    CSV.parse(DATA.read,:col_sep=>';',:headers=>true,
        :skip_lines=> /^#/  #Mark comments!
      ) do |row|
      p row
    end
    #~ 
    __END__
    #~ Comment
    #~ More comment
    a;b;c;d
    1;2;3;4
    #~ More comment
    1;2;3;4
    #~ More comment
    1;2;3;4
    

    The result is

    #<CSV::Row "a":"1" "b":"2" "c":"3" "d":"4">
    #<CSV::Row "a":"1" "b":"2" "c":"3" "d":"4">
    #<CSV::Row "a":"1" "b":"2" "c":"3" "d":"4">
    

    In your case the csv contains a date, so you may use:

    require 'csv'
    
    CSV.parse(DATA.read,:col_sep=>';',:headers=>true,
        :skip_lines=> /^\d\d\d\d-\d\d-\d\d$/  #Skip line with date only
      ) do |row|
      p row
    end
    #~ 
    __END__
    2016-03-19
    a;b;c;d
    1;2;3;4
    1;2;3;4
    1;2;3;4
    

    or you could use more extend starting lines:

    require 'csv'
    
    CSV.parse(DATA.read,:col_sep=>';',:headers=>true,
        :skip_lines=> /^Created by/  #Skip line with date only
      ) do |row|
      p row
    end
    
    __END__
    Created by test.rb on 2016-03-19
    a;b;c;d
    1;2;3;4
    1;2;3;4
    1;2;3;4
    
    0 讨论(0)
  • 2020-12-07 03:25

    This simple code worked for me. You can read a CSV file and ignore its first line which is the header or field names:

    CSV.foreach(File.join(File.dirname(__FILE__), filepath), headers: true) do |row|
        puts row.inspect
    end
    

    You can do what ever you want with row. Don't forget to use headers: true

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-07 03:34

    I don't think there's an elegant way of doing it, but it can be done:

    require "csv"
    
    # Create a stream using the original file.
    # Don't use `textmode` since it generates a problem when using this approach.
    file = File.open "file.csv"
    
    # Consume the first CSV row.
    # `\r` is my row separator character. Verify your file to see if it's the same one.
    loop { break if file.readchar == "\r" }
    
    # Create your CSV object using the remainder of the stream.
    csv = CSV.new file, headers: true
    
    0 讨论(0)
提交回复
热议问题