Changing field separator/delimiter in exported CSV using Ruby CSV

Here’s an example using a tab instead.

To a file:

CSV.open("myfile.csv", "w", {:col_sep => "\t"}) do |csv|
  csv << ["row", "of", "CSV", "data"]
  csv << ["another", "row"]
  # ...
end

To a string:

csv_string = CSV.generate(:col_sep => "\t") do |csv|
  csv << ["row", "of", "CSV", "data"]
  csv << ["another", "row"]
  # ...
end

Here’s the current documentation on CSV: http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

Leave a Comment