How to save a hash into a CSV

前端 未结 7 1553
名媛妹妹
名媛妹妹 2020-12-08 04:26

I am new in ruby so please forgive the noobishness.

I have a CSV with two columns. One for animal name and one for animal type. I have a hash with all the keys being

相关标签:
7条回答
  • 2020-12-08 05:03

    Try this:

    require 'csv'
    h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine' }
    CSV.open("data.csv", "wb") {|csv| h.to_a.each {|elem| csv << elem} }
    

    Will result:

    1.9.2-p290:~$ cat data.csv 
    dog,canine
    cat,feline
    donkey,asinine
    
    0 讨论(0)
提交回复
热议问题