Skip to content

Instantly share code, notes, and snippets.

@honorelsu
Created December 29, 2014 21:05
Show Gist options
  • Save honorelsu/705979fa00e9a4c5f0d3 to your computer and use it in GitHub Desktop.
Save honorelsu/705979fa00e9a4c5f0d3 to your computer and use it in GitHub Desktop.
CSV to Array of Hashes
require 'CSV'
require 'easy_diff'
dcsexport = ARGV[0]
opsexport = ARGV[1]
def csv_to_array(file_location)
csv = CSV::parse(File.open(file_location, 'r') {|f| f.read})
fields = csv.shift
csv.collect {|record| Hash[*fields.zip(record).flatten]}
end
dcs = csv_to_array(dcsexport)
ops = csv_to_array(opsexport)
puts "The exported file is of type #{dcs.class}"
puts "The exported file's first row is of type #{dcs.first.class}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment