Rectangle 27 0

ruby Parse CSV file with header fields as attributes for each row?


CSV.foreach('my_file.csv', :headers => true) do |row|
  puts row['foo'] # prints 1 the 1st time, "blah" 2nd time, etc
  puts row['bar'] # prints 2 the first time, 7 the 2nd time, etc
end

As an aside, for Ruby 1.8.x FasterCSV is what you need to use the above syntax.

FasterCSV was incorporated into Ruby, I think it was in Ruby 1.9+.

It's not dot syntax but it is much nicer to work with than numeric indexes.

Using Ruby 1.9 and above, you can get a an indexable object:

Note
Rectangle 27 0

ruby Parse CSV file with header fields as attributes for each row?


1,2,3
blah,7,blam
4,5,6
CSV.foreach("#{Rails.root}/db/data.csv", {:headers => true, :header_converters => :symbol}) do |row|
  puts "#{row[:foo]},#{row[:bar]},#{row[:baz]}"
end
require 'fastercsv'
CSV.foreach("#{Rails.root}/db/data.csv", {:headers => true, :header_converters => :symbol}) do |row|
  puts "#{row[:foo]},#{row[:bar]},#{row[:baz]}"
end
row.headers

:headers => true treats the first row as a header instead of a data row. :header_converters => :symbolize parameter then converts each cell in the header row into Ruby symbol.

Based on the CSV provided by the Poul (the StackOverflow asker), the output from the example code above will be:

Depending on the characters used in the headers of the CSV file, it may be necessary to output the headers in order to see how CSV (FasterCSV) converted the string headers to symbols. You can output the array of headers from within the CSV.foreach.

Here is an example of the symbolic syntax using Ruby 1.9. In the examples below, the code reads a CSV file named data.csv from Rails db directory.

Marcos - If the CSV has been converted into an array, you may have lost the the hashes (symbols). If this is the case, just reference the cell by the column number e.g. myrow[0].

So I loaded CSV file into an array with only allstocks << row inside the loop. How do I read one cell myrow[:company] where myrow[:ticker] == "ANAD"? There is only one record and ticker is my key field anyway.

Note
Rectangle 27 0

ruby Parse CSV file with header fields as attributes for each row?


CSV.foreach('my_file.csv', :headers => true) do |row|
  puts row['foo'] # prints 1 the 1st time, "blah" 2nd time, etc
  puts row['bar'] # prints 2 the first time, 7 the 2nd time, etc
end

As an aside, for Ruby 1.8.x FasterCSV is what you need to use the above syntax.

FasterCSV was incorporated into Ruby, I think it was in Ruby 1.9+.

It's not dot syntax but it is much nicer to work with than numeric indexes.

Using Ruby 1.9 and above, you can get a an indexable object:

Note
Rectangle 27 0

ruby Parse CSV file with header fields as attributes for each row?


1,2,3
blah,7,blam
4,5,6
CSV.foreach("#{Rails.root}/db/data.csv", {:headers => true, :header_converters => :symbol}) do |row|
  puts "#{row[:foo]},#{row[:bar]},#{row[:baz]}"
end
require 'fastercsv'
CSV.foreach("#{Rails.root}/db/data.csv", {:headers => true, :header_converters => :symbol}) do |row|
  puts "#{row[:foo]},#{row[:bar]},#{row[:baz]}"
end
row.headers

:headers => true treats the first row as a header instead of a data row. :header_converters => :symbolize parameter then converts each cell in the header row into Ruby symbol.

Based on the CSV provided by the Poul (the StackOverflow asker), the output from the example code above will be:

Depending on the characters used in the headers of the CSV file, it may be necessary to output the headers in order to see how CSV (FasterCSV) converted the string headers to symbols. You can output the array of headers from within the CSV.foreach.

Here is an example of the symbolic syntax using Ruby 1.9. In the examples below, the code reads a CSV file named data.csv from Rails db directory.

Marcos - If the CSV has been converted into an array, you may have lost the the hashes (symbols). If this is the case, just reference the cell by the column number e.g. myrow[0].

So I loaded CSV file into an array with only allstocks << row inside the loop. How do I read one cell myrow[:company] where myrow[:ticker] == "ANAD"? There is only one record and ticker is my key field anyway.

Note
Rectangle 27 0

ruby Parse CSV file with header fields as attributes for each row?


"foo","bar","baz"
1,2,3
"blah",7,"blam"
4,5,6
dump
load
require 'virgola'

class FooBar
  include Virgola

  attribute :foo
  attribute :bar
  attribute :baz
end

csv = <<CSV
"foo","bar","baz"
1,2,3
"blah",7,"blam"
4,5,6
CSV

foo_bars = FooBar.parse(csv).all
foo_bars.each { |foo_bar| puts foo_bar.foo, foo_bar.bar, foo_bar.baz }

Given your CSV contents, mapping them to an array of FooBar objects is pretty straightforward:

Something like this. It's a pretty cool feature actually! gist.github.com/3188109

Note
Rectangle 27 0

ruby Parse CSV file with header fields as attributes for each row?


"foo","bar","baz"
1,2,3
"blah",7,"blam"
4,5,6
dump
load
require 'virgola'

class FooBar
  include Virgola

  attribute :foo
  attribute :bar
  attribute :baz
end

csv = <<CSV
"foo","bar","baz"
1,2,3
"blah",7,"blam"
4,5,6
CSV

foo_bars = FooBar.parse(csv).all
foo_bars.each { |foo_bar| puts foo_bar.foo, foo_bar.bar, foo_bar.baz }

Given your CSV contents, mapping them to an array of FooBar objects is pretty straightforward:

Something like this. It's a pretty cool feature actually! gist.github.com/3188109

Note