Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6642137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:53:42+00:00 2026-05-25T23:53:42+00:00

The incoming data file(s) contain malformed CSV data such as non-escaped quotes, as well

  • 0

The incoming data file(s) contain malformed CSV data such as non-escaped quotes, as well as (valid) CSV data such as fields containing new lines. If a CSV format error is detected I would like to use an alternative routine on that data.

With the following sample code (abbreviated for simplicity)

FasterCSV.open( file ){|csv|
  row = true
  while row
    begin
      row = csv.shift
      break unless row
      # Do things with the good rows here...

    rescue FasterCSV::MalformedCSVError => e
      # Do things with the bad rows here...
      next
    end
  end
}

The MalformedCSVError is caused in the csv.shift method. How can I access the data that caused the error from the rescue clause?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:53:42+00:00Added an answer on May 25, 2026 at 11:53 pm
    require 'csv' #CSV in ruby 1.9.2 is identical to FasterCSV
    
    # File.open('test.txt','r').each do |line|
    DATA.each do |line|
      begin
        CSV.parse(line) do |row|
          p row #handle row
        end
      rescue  CSV::MalformedCSVError => er
        puts er.message
        puts "This one: #{line}"
        # and continue
      end
    end
    
    # Output:
    
    # Unclosed quoted field on line 1.
    # This one: 1,"aaa
    # Illegal quoting on line 1.
    # This one: aaa",valid
    # Unclosed quoted field on line 1.
    # This one: 2,"bbb
    # ["bbb", "invalid"]
    # ["3", "ccc", "valid"]   
    
    __END__
    1,"aaa
    aaa",valid
    2,"bbb
    bbb,invalid
    3,ccc,valid
    

    Just feed the file line by line to FasterCSV and rescue the error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have unpredictable frequency of Incoming csv data file which I need to store
I need to perform update/insert simultaneously changing structure of incoming data. Think about Shops
My Perl app and MySQL database now handle incoming UTF-8 data properly, but I
I need to process the incoming predefined ASN format data(coming from verity of clients
I'm having difficulties displaying data in a UITextView in iPhone programming. I'm analyzing incoming
I have the following directory structure: +-archive +-a +-data.txt +-b +-data.txt +-incoming +-a +-data.txt
I have a incoming stream of bytes (unsigned char) from either a file or
A servlet class handles the incoming request object, fetch data & store into StringBuilder/StringBuffer
My program is logging data to a file, at the same time a user
I have an application that runs (listens to incoming TCP data) from the command

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.