Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSV --> CSV in Ruby

Tags:

ruby

csv

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

like image 546
mbm Avatar asked Apr 28 '26 08:04

mbm


1 Answers

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
like image 103
Bill Dueber Avatar answered Apr 30 '26 00:04

Bill Dueber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!