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

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

What's the best way?

Speed ​​is not important. There is clarity.

+1


source to share


1 answer


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 row 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 

      

+7


source







All Articles