Splashlin
Splashlin

Reputation: 7585

Create Tab Delimited ASCII file in Rails

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?

Upvotes: 3

Views: 1814

Answers (3)

shingara
shingara

Reputation: 46914

You can use FasterCVS too ( include in ruby 1.9 )

http://fastercsv.rubyforge.org/

Upvotes: 2

Aurril
Aurril

Reputation: 2469

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

Upvotes: 1

uzzz
uzzz

Reputation: 559

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

Upvotes: 1

Related Questions