$:.push("~/ownCloud/CodeSnippets") require "csvx.rb" require "util.rb" require "json" input = File.csv("predictions.csv") headers = input.shift.every(2, 1).map { |h| h.gsub(/[^0-9a-zA-Z]/, "_").gsub(/_+/,"_") } p headers # outputs = Hash.new { |hash, key| hash[key] = File.new("output_#{headers[key]}.csv", "w+") } ret = input.map do |l| (0..(l.length / 2).floor).map do |i| { x: l[i*2], y: l[i*2+1], c: i } if l[i*2] != "" end.compact end.flatten File.open("predictions.json", "w+") do |f| f.puts(ret.to_json) end # outputs.values.each { |f| f.close }