For some reason, it is appending the column header to each value and
also printing a blank row to start each column. My code is below. Any
help is much appreciated. Essentially I read the CSV into a hash where
the key is the column header and the element is an array of values from
that column. I then run .uniq! on each array in the hash and print the
results to a file.
require 'rubygems'
require 'faster_csv'
infile = "xyz.csv"
uniques = {}
FCSV.open(infile, :headers => true).each do |row|
row.each_with_index do |element,j|
uniques[row.headers[j]] ||= []
uniques[row.header[j]] << element
end
end
uniques.each do |key,element|
element.uniq!
end
File.open("unique_output.txt","w+") do |out|
uniques.each_key do |key|
out.write "Column: #{key}\n"
uniques[key].each do |element|
out.write " #{element}\n"
end
end
end
For some reason, it is appending the column header to each value and
also printing a blank row to start each column. My code is below. Any
help is much appreciated. Essentially I read the CSV into a hash where
the key is the column header and the element is an array of values from
that column. I then run .uniq! on each array in the hash and print the
results to a file.
require 'rubygems'
require 'faster_csv'
infile = "xyz.csv"
uniques = {}
FCSV.open(infile, :headers => true).each do |row|
row.each_with_index do |element,j|
uniques[row.headers[j]] ||=
uniques[row.header[j]] << element
end
end
uniques.each do |key,element|
element.uniq!
end
File.open("unique_output.txt","w+") do |out|
uniques.each_key do |key|
out.write "Column: #{key}\n"
uniques[key].each do |element|
out.write " #{element}\n"
end
end
end