views:

253

answers:

3

I need to create a tab delimited ASCII file from a table (Hits) in my DB. I can already export this table into a CSV file. What's the best way to go about doing this? Is there a way to easily create this in rails?

A: 

Assume the CSV data is in "something.csv" and delimited by ","

require 'csv'
File.open("tab_seperated.txt", "w+") do |f|
  f << CSV.parse(File.read("something.csv")).map{|row| row.join("\t")}.join("\n")
end
Aurril
A: 

you can do it directly from rails console (or put it to the rake task) this way:

File.open('file.txt', 'w') do |f|
  f.puts Hits.all.map { |h| [h.value1, h.value2].join("\t") }.join("\n")
end
uzzz
+2  A: 

You can use FasterCVS too ( include in ruby 1.9 )

http://fastercsv.rubyforge.org/

shingara