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
You can do this
text = File.readlines("file.csv")[1..-1].join()
csv = CSV.parse(text, headers: true)
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
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
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
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