Using Ruby 1.9.2 I need to parse a CSV file, and output lines with each header key and individual value paired together, with line numbers.
header: key1, key2, key3
row1: a, b, c
row2: d, , f
expected output:
1
key1 a
key2 b
key3 c
2
key1 d
key2
key3 f
Right now working on just combining the header with the values, and I’m failing pretty hard.
require 'csv'
header = File.open('TEXT.CSV', &:readline)
keys = header.split(",")
values = CSV.read("TEXT.CSV")
def combine(a,b)
zipped = a.zip(b)
Hash[zipped]
end
keyvalue = values.each do |i|
combine(keys,i)
end
Any ideas what I did wrong in there?
creates: