Ruby Write in CSV file by columns from arrays? -
i generate csv file severals arrays. code:
require 'csv' csv.open("csvfile.csv", "ab") |csv| csv << [array1] csv << [array2] csv << [array3] end
i need output format:
array1,array2,array3 array1,array2,array3 array1,array2,array3 array1,array2,array3 array1,array2,array3
thx help
according post, did mean array1, array2, array3 stores values of 3 columns of table, , row index identified index of values in these arrays? can first group columns together, transpose
on 2-d array , write csv file row row.
require 'csv' table = [array1, array2, array3].transpose csv.open('csvfile.csv', 'ab') |csv| table.each |row| csv << row end end
you'll csv file this:
array1[0], array2[0], array3[0] array1[1], array2[1], array3[1] array1[2], array2[2], array3[2] ...
Comments
Post a Comment