mbm
mbm

Reputation: 1922

TSV --> CSV in Ruby

In Ruby, what's the most efficient way to convert a file of tab-separated values into CSV?

Upvotes: 3

Views: 3360

Answers (1)

Bill Dueber
Bill Dueber

Reputation: 2706

Use FasterCSV

require 'rubygems'
require 'fastercsv'

FasterCSV.open("path/to/file.csv", "w") do |csv|
  File.open("/path/to/file.tsv") do |f|
    f.each_line do |tsv|
      tsv.chomp!
      csv << tsv.split(/\t/)
    end
  end
end

Upvotes: 4

Related Questions