Skip to content



Parsing CSV files with ruby FasterCSV : An example

Need to parse csv files in ruby, skipping the first row (usually some header)?. Use FasterCSV. (Yes, its faster). Install it

$ sudo gem install fastercsv

Here is one solution

#!/usr/bin/ruby -w

require 'rubygems'
require 'faster_csv'

#put everything in an array
array_of_arrays = FasterCSV.read("some_file.csv") 

# remove headers
array_of_arrays.slice!(0) 

array_of_arrays.each{|row|
  puts row
}

Another solution is

#!/usr/bin/ruby -w

require 'rubygems'
require 'faster_csv'

# setting the headers option to true or :first_row gets rid of the headers
FasterCSV.foreach('some_file.csv', :headers => :first_row) {|row|
    puts row
}

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*