-
-
Save Tassandar/3803656 to your computer and use it in GitHub Desktop.
How to parse CSV data with Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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