Skip to content

Instantly share code, notes, and snippets.

@atiw003
Created August 14, 2010 13:40
Show Gist options
  • Save atiw003/524312 to your computer and use it in GitHub Desktop.
Save atiw003/524312 to your computer and use it in GitHub Desktop.
#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|
p 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]
break
end
#Parsing with the ccsv library
#ccsv is hosted on GitHub.
require 'rubygems'
require 'ccsv'
Ccsv.foreach(file) do |values|
puts values[0]
end
#Parsing with the CSVScan library
#CSVScan can be downloaded from here.
require "csvscan"
open("data.csv") {|io|
CSVScan.scan(io) {|row|
p row
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment