Posted By

legrandkay on 02/12/14


Tagged

array ruby csv


Versions (?)

Turn CSV With Headers Into Array Of Hashes


 / Published in: Ruby
 

To be improved to work with fasterCSV

  1. require 'csv'
  2.  
  3. csv_data = CSV.read 'data.csv'
  4. headers = csv_data.shift.map {|i| i.to_s }
  5. string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }
  6. array_of_hashes = string_data.map {|row| Hash[*headers.zip(row).flatten] }

Report this snippet  

You need to login to post a comment.