Ruby on Rails Migrating from CSV to FasterCSV

I currently have the following code for parsing a csv file using the csv standard library

@parsed_file=CSV::Reader.parse(params[:dump][:file])
@parsed_file.each  do |row|
#some code
end

I want to move this to a faster csv to increase speed. Does anyone know the equivalent of the above for FasterCSV?

thank

0
source share
2 answers
CSV::Reader.parse(File.open('file.csv')){|row| puts row} 
or
CSV::Reader.parse("some, content\nanother, content"){|row| puts row} 

and

FasterCSV.parse(File.open('file.csv')){|row| puts row}
or
FasterCSV.parse("some, content\nanother, content"){|row| puts row}

are equivalent.

But

FasterCSV.read('filename') 

accepts the file name as a parameter and reads and analyzes data from the file, however you discard the contents of the file when you pass data in the parameter

@parsed_file = FasterCSV.parse(params[:dump][:file])
@parsed_file.each do |row| 
  puts row
  # and do some operations
end

should work fine.

+2
source

To do this using the file path (as you think):

FasterCSV.read(params[:dump][:file])

FasterCSV (, ).

+1

All Articles