Created
August 14, 2010 13:40
-
-
Save atiw003/524312 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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