in reply to CSV cleanup problem...

I was going to suggest that you transform

"title", "Some Name, "some wierd title"", "555-555-5555"
to
'title'|'Some Name, "some wierd title"'|'555-555-5555'
using
$line =~ s[^"|"$][']g; $line =~ s[",\s"]['|']g;

And then set

my $csv = Text::CSV_XS->new({ 'quote_char' => "'", 'sep_char' => '|', });

But for some reason I do not understand, Text::CSV_XS fails to parse using these parameters.

As far as I can tell from the docs this combination ought to work, but it doesn't, and I can't find any way to determine what it is that it doesn't like.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Replies are listed 'Best First'.
Re^2: CSV cleanup problem...
by jZed (Prior) on Jun 14, 2005 at 05:56 UTC
    You need to add escape_char => q{'} or somesuch, otherwise the quote marks are treated as escapes. In the next release I intend to have the escape_char default to whatever the sep_char is set to rather than to " as it is now. This parses your sample correctly for me:
    #!perl -w use strict; use Text::CSV_XS; use IO::Scalar; my $str = q{'title'|'Some Name, "some wierd title"'|'555-555-5555'}; my $csv = Text::CSV_XS->new( { binary=>1, quote_char=>q{'}, sep_char=>q{|}, escape_char=>q{'}, } ); my $fh = IO::Scalar->new(\$str); while (my $cols = $csv->getline($fh)) { last unless @$cols; printf "%s\n", join "\n",@$cols; }