Search code examples
ruby-on-railsrubycsv

how to skip/ignore malformed CSV when using CSV.foreach?


I tried to read large csv file

but the csv are on bad condition

so some of it's line throwing CSV::MalformedCSVError

I just want to ignore the error line and move onto next line

I tried to add begin rescue but seems my code is not working, it stopped at the error

my current code

require 'csv'    

begin
  CSV.foreach(filename, :headers => true) do |row|
    Moulding.create!(row.to_hash)
  end
rescue
  next
end

Solution

  • I don't think you can do it with the foreach method because the exception does not seem to be raised within the block but rather within the foreach method itself, but something like this should work. In this case the exception is raised on the call to shift, which you can then rescue out of.

    require 'csv'    
    
    csv_file = CSV.open("test.csv", :headers => true)
    loop do
        begin
          row = csv_file.shift
          break unless row 
          p row
        rescue CSV::MalformedCSVError
          puts "skipping bad row"
        end
    end
    

    BTW your code above does not run because when you moved begin rescue to surround the foreach method , next is no longer valid in that context. Commenting out the next statement the code runs but when the exception is raised in the foreach method the method just ends and program moves on to the rescue block and does not read any more lines from the file.