5

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 on the second row, so I need to be able to skip the first row when iterating over it. I tried using slice but that converts the CSV to an array and I really want to read it as CSV so I can take advantage of headers.

1
  • What is your input data? What do you want your output data to look like? What is the minimal code you wrote that demonstrates what you're trying to do and why doesn't it work? Or, are you hoping someone will write the code for you? Nov 12, 2014 at 21:36

6 Answers 6

7

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
4

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
1

You can do this

text = File.readlines("file.csv")[1..-1].join()
csv = CSV.parse(text, headers: true)
0

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

For the posterity: sometimes the first lines are present but with empty values (a row with ,,,,,,,,,, before the headers), so the solution is to remove those by doing this:

require 'csv'

CSV.parse(content, headers: true, skip_lines: /^(\s*,\s*)*$/)

This solution will work, no matter how many rows with empty values before the headers are present. Also it will remove any empty rows after the headers, so be careful and double check if that works for you.

P.S.: Change the comma (,) if you're separating with other char

-1

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

1
  • The question asks how to process a CSV where the first line is not the header, but some junk that must be ignored: The header is the second line. May 16, 2018 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.