Skip to content

Instantly share code, notes, and snippets.

@Tassandar
Created September 29, 2012 10:30
Show Gist options
  • Save Tassandar/3803656 to your computer and use it in GitHub Desktop.
Save Tassandar/3803656 to your computer and use it in GitHub Desktop.
How to parse CSV data with Ruby
#Ruby alternatives for parsing CSV files
# Ruby String#split (slow)
# Ruby CSV (slow)
# FasterCSV (ok, recommended)
# ccsv (fast & recommended if you have control over CSV format)
# CSVScan (fast & recommended if you have control over CSV format)
# Excelsior (fast & recommended if you have control over CSV format)
#CSV library benchmarks can be found here and here
#Parsing with plain Ruby
filename = 'data.csv'
file = File.new(filename, 'r')
file.each_line("\n") do |row|
columns = row.split(",")
break if file.lineno > 10
end
#This option has several problems…
#Parsing with the CSV library
require 'csv'
CSV.open('data.csv', 'r', ';') do |row|
puts row
end
#Parsing with the FasterCSV library
require 'rubygems'
require 'faster_csv'
FasterCSV.foreach("data.csv", :quote_char => '"', :col_sep =>';', :row_sep =>:auto) do |row|
puts row[0]
end
#Parsing with the ccsv library
require 'rubygems'
require 'ccsv'
Ccsv.foreach(file) do |values|
puts values[0]
end
#Parsing with the CSVScan library
require "csvscan"
open("data.csv") do |io|
CSVScan.scan(io) do|row|
puts row
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment