Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Duane Johnson http://blog.inquirylabs.com/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Transpose CSV Dataset Using FasterCSV

Takes a dataset such as:
   1  
   2   $10,000 	 $15,000 	 $20,000 
   3   $30,000 	 $35,000 	 $40,000 
   4  1	1	2
   5  1	1	2
   6  1	1	2
   7  2	2	2


and converts it to
   1  
   2  1000000,3000000,1,1,1,2
   3  1500000,3500000,1,1,1,2
   4  2000000,4000000,2,2,2,2


   1  
   2    def transpose_csv_fixture_file!(path)
   3      converter = proc{ |v| v =~ /\$/ ? v.gsub(/[$,]/, "").to_i * 100 : v.to_f }
   4      data = FasterCSV.read(path, :col_sep => "\t", :converters => converter ).transpose
   5      FasterCSV.open(path, "w") { |csv| data.each{ |line| csv << line } }
   6    end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS