in reply to parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?

Also note that Text::xSV will solve the problem without assuming fixed width.

That solution also handles embedded returns correctly. (In case anyone ever pulls that on you.)

  • Comment on Re (tilly) 1: parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?

Replies are listed 'Best First'.
(tye)Re3: parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?
by tye (Sage) on Oct 31, 2001 at 23:14 UTC

    Note that Text::xSV can't handle (quite reasonably) the unescaped embedded quote characters inside of quoted fields that amelinda's data appear to have.

            - tye (but my friends call me "Tye")
      It could be made to though. That could even be made configurable, though I would prefer it to default to not for the simple reason that I don't like it.

      A bigger problem is that I only consider " a quote character. I will need to check whether ' also qualifies for Microsoft products. If it does I should make it handle that as well. (They never bother writing it, so I didn't either.) If not, then I could make that configurable as well...

        You seem to be assuming that the unescaped quote will never be followed by a comma (or white space and then a comma, or perhaps a newline). Actually, amelinda's use of the word "other" makes me suspect that this assumption is likely true in this particular case. (:

        But I felt it important that readers not get the wrong impression about how well such "not really CSV" data can ever be handled in general. You have to draw the line somewhere. As I said, I find it quite reasonable to draw the line at "your embedded quotes must be escaped". But if you cross that line, there still has to be another line such as "your unescaped embedded quotes must not be followed by...".

        To be honest, I never seriously considered such an option. And then when you mentioned it, the phrase "that way lies madness" (in an appropriately Halloween voice) came to mind. Then I switched to, "well, that would be fairly easy to add." Then I thought about exactly how to do it and recalled how many times I've seen brilliant people that were regex experts get the "match C-style comments" regex wrong and started to have doubts again.

        Perhaps trying something like /'([^']|'(?! *[,\n]))*'/ to match single-quoted fields would work. But I think I'd probably backtrack at this point and say that adding support for that has too high of a risk of introducing bugs. Perhaps making it optional could aleviate that risk when the option isn't selected. Perhaps you have more confidence in your ability to solve that problem correctly than I do in mine. :)

                - tye (but my friends call me "Tye")
Re: Re (tilly) 1: parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?
by amelinda (Friar) on Nov 01, 2001 at 03:39 UTC
    Actually, I'm more interested in how it could handle the unescaped embedded comma in one of the fields.

    If only splitting (or Text::xSVing) would handle that extraneous comma, i could worry about stripping off the quote marks later. It was the combination the unescaped embedded comma, the unescaped embedded quote, and the fact that only some fields were marked off in quotes that led me down the road to unpack.

      Embedded commas aren't a problem for CSV. The whole point of putting quotes around fields in the CSV is so that embedded commas can be dealt with. Embedded quotes aren't a problem if they are escaped (by doubling them).

      If you are curious how this is done, then I encourage you to download Text::CSV and/or Text::xSV (or just look at Text::xSV locally) and look for yourself. (:

              - tye (but my friends call me "Tye")