Turn CSV With Headers Into Array Of Hashes

/ Published in: Ruby
Save to your folder(s)

To be improved to work with fasterCSV

Copy this code and paste it in your HTML
  1. require 'csv'
  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


RSS Icon Subscribe to comments

You need to login to post a comment.