Parsing and creating csv files in Ruby using FasterCSV
Posted: April 20th, 2011 | Author: Dax | Filed under: Ruby | No Comments »This tutorial shows how you can read and write csv files in Ruby. I’m going to use a plugin called FasterCSV.
To install FasterCSV, type in your terminal:
sudo apt-get install fastercsv
In your ruby code:
require 'rubygems'
require 'faster_csv'
# Reading csv file
FasterCSV.foreach(csv_file, :quote_char => '"', :col_sep => ',', :row_sep => :auto) do |row|
puts row[0] # Output first cell
end
# Writing data into a csv file
FasterCSV.open("report.csv", "w") do |csv|
csv << ['Bulls','62','20']
csv << ['Spurs','61','21']
end
There are also other alternatives for parsing and writing csv files in Ruby. You can use CSV, CSVScan and CCSV
Leave a Reply