This works but for some reason the comma is missing. data gets inserted as
GAURISH SHARMA
97
69
69
975
PASS
with each value written in new line. I wonder Where did the commas go? or do those have to manually inserted?
I am trying to write hash values using the following code
CSV.open("resultdata.csv", "a") do |csv|
h.each do |key, value|
csv << value
end
end
and here are the contents of the h hash
{:name=>"GAURISH SHARMA",
:ca=>"97",
:cb=>"69",
:ba_lab=>"69",
:bb_lab=>"69",
:grand_total=>"975",
:result=>"PASS"}
This code looks correct but upon running this code, generates the following errors:
/home/gaurish/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/csv.rb:1729:in `<<': undefined method `map' for "GAURISH SHARMA":String (NoMethodError)
from /home/gaurish/Dropbox/code/projects/ra/result.rb:35:in `block (2 levels) in <main>'
from /home/gaurish/Dropbox/code/projects/ra/result.rb:34:in `each'
from /home/gaurish/Dropbox/code/projects/ra/result.rb:34:in `block in <main>'
from /home/gaurish/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/csv.rb:1354:in `open'
from /home/gaurish/Dropbox/code/projects/ra/result.rb:33:in `<main>'
Any idea what is wrong here?
When you do
csv << value, you are writing a string to a file.The CSV library makes it possible to write an array to a file (in a certain form). The best way to get an array with all values from a hash is to use the
valuesmethod btw. So:resultdata.csv will look like