Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Transpose CSV Dataset Using FasterCSV

DZone's Guide to

Transpose CSV Dataset Using FasterCSV

·
Free Resource
Takes a dataset such as:

 $10,000 	 $15,000 	 $20,000 
 $30,000 	 $35,000 	 $40,000 
1	1	2
1	1	2
1	1	2
2	2	2


and converts it to

1000000,3000000,1,1,1,2
1500000,3500000,1,1,1,2
2000000,4000000,2,2,2,2



  def transpose_csv_fixture_file!(path)
    converter = proc{ |v| v =~ /\$/ ? v.gsub(/[$,]/, "").to_i * 100 : v.to_f }
    data = FasterCSV.read(path, :col_sep => "\t", :converters => converter ).transpose
    FasterCSV.open(path, "w") { |csv| data.each{ |line| csv << line } }
  end
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}