I need to parse invalid CSV in a situation where it's possible to get the source to generate valid CSV. The problem is that quoting isn't handled right, so this may appear in the file:

  "call from "friend""

My best idea for handling it is to try to pre-parse it to make it valid, and then hand the result to Text::CSV_XS to finish the job. The logic for "fixing" it might be like this:

- If a quote character appears at the beginning or end of a line, or next to a comma, consider it a part of the file delimiter. Otherwise, consider it an internal quote and escape it properly.

I realize there are edge cases that wouldn't be handled by this logic, but it does seem like a case that difficult to correct perfectly automatically.

My hope is that someone might now of a CSV parsing tool that tolerates this particular kind of broken-ness.

Thanks!


In reply to Recommendations for parsing invalid CSV by markjugg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.