rubycsvfastercsv

Importing a huge csv data file and using the header to access columns using Ruby


I need to import a huge csv data file (6880 columns) and I need to be able use the column headers to access it.

What's the best way?

Speed isn't important. Clarity is.


Solution

  • FasterCSV (also available as CSV in Ruby 1.9 standard library) should be able to do the trick. You can use column headers to access a row's data:

    require 'fastercsv'
    FasterCSV.foreach(csv_file, {:headers => true, :return_headers => false, :header_converters => :symbol, :converters => :all} ) do |row|
        puts row[:some_column_header] # Would be "Some Column Header" in the csv file.
    end