sziep/csv_by_column
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
iterate CSV data by colum easily
Created by Stephan Ziep on 2011-04-28.
Copyright 2011 Stephan Ziep. You can redistribute or modify this code
under the terms of Ruby's license.
See CSV for documentation.
== Description
This small addition to Rubys default CSV class, enhances it in a way
that lets you iterate over columns in a nice way syntacticly
E.g: given a csv dataset like:
Name,Nickname,Email,Message,ID
Stephan,nick1,stephan@mail.de,"A first message",3
Nico,nick2,nico@mail.de,"A second message",6
Richard,nick3,richard@mail.de,"A third message",67
Marc,nick4,marc@mail.de,"A fourth message",1234
You can directly refer to the header (downcased) in the foreach method
like this:
OPTIONS = { converters: :all,
headers: true,
header_converters: :downcase}
CSV.foreach_message("data.csv", OPTIONS).each do |msg|
p msg
end
CSV.foreach_message_and_nickname("data.csv", OPTIONS).each do |msg|
p msg
end
or you use the foreach_column method directly:
CSV.foreach_column("data.csv", OPTIONS, "email") do |mail|
p mail
end