Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

CSV Parser, with and without encapsulation

by tosaiju (Acolyte)
on Oct 24, 2012 at 10:41 UTC ( #1000599=perlquestion: print w/replies, xml ) Need Help??
tosaiju has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Which is the best CSV parser?
I tried using Text::CSV - which works good for any Standard CSV records.
Recently I got a problem where in a file - the records are sometimes encapsulated and sometimes not. Can we handle this in one Parser. Also intrestingly this worked for me while using Text-CSV v1.13; but failed when using Text-CSV v1.21.

The secnario what I have is something like below,
field1,field2,some desc with inch 10",field3
field1,field2,soem desc,field3
field1,field2,"some desc with sep, and other",field3

CSV object was set with encapsulation(") and this works well while using Text-CSV v1.13, but not with Text-CSV v1.21
Appreciate your views.

Many Thanks,
  • Comment on CSV Parser, with and without encapsulation

Replies are listed 'Best First'.
Re: CSV Parser, with and without encapsulation
by Anonymous Monk on Oct 24, 2012 at 10:48 UTC
      Thanks, this really helped. Now the only problem might be if escape_char is same as quote_char. Hope all valid/invalid things will not happen in 1 file.
Re: CSV Parser, with and without encapsulation
by Tux (Abbot) on Oct 25, 2012 at 08:14 UTC

    The "best" is of course Text::CSV_XS :)

    Text::CSV is just a wrapper over Text::CSV_XS and/or Text::CSV_PP.

    If your CSV data is mixed and invalid, chances are that you might not find a combination of options that will work on all records, but a good start for what you describe would be something like:

    my $csv = Text::CSV_XS->new ({ binary => 1, allow_loose_quotes => 1, escape_char => undef, auto_diag => 1, });

    Please carefully read the manual for all allow_*** options.

    Enjoy, Have FUN! H.Merijn

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1000599]
Approved by Ratazong
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-10-16 21:27 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (89 votes). Check out past polls.