Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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
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 (Monsignor) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-07-23 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (131 votes), past polls