Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: line ending problem Text::CSV alternative Text::ParseWords?

by Tux (Canon)
on Oct 06, 2009 at 12:17 UTC ( [id://799461]=note: print w/replies, xml ) Need Help??


in reply to line ending problem Text::CSV alternative Text::ParseWords?

When parsing CSV with Text::CSV_XS or Text::CSV don't specify the eol attribute at all, and getline () most likely will do-the-right-thing.

open my $fh, "<", $file or die "$file: $!"; # Don't use auto_diag on older Text::CSV_XS my $csv_in = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); my $csv_out = Text::CSV_XS->new ({ binary => 1, sep_char => "\t", eol + => $/ }); while (my $row = $csv->getline ($fh)) { $csv_out->print (*STDOUT, [ @{$row}[2, 3, 0, 4] ]); } $csv->eof () or $csv->error_diag (); # Not needed with auto_diag close $fh;

update: filled in the correct output syntax for a array-ref-slice

Important update: I was wrong. A single \r is not a valid line ending in the default configuration according to the CSV specs. In that case you'd have to specify that with eol => "\r". I will see if I can make it legal if the system $/ already is a "\r".


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: line ending problem Text::CSV alternative Text::ParseWords?
by GertMT (Hermit) on Oct 06, 2009 at 12:29 UTC

    that gives me:

    # CSV_XS ERROR: 2023 - EIQ - QUO character not allowed

    and nothing further.

    If I leave out the auto_diag = 1 part I get nothing back on the screen.

      From the man page:

      2023 "EIQ - QUO character not allowed" Sequences like ""foo "bar" baz",quux" and "2023,",2008-04-05, +"Foo, Bar",\n" will cause this error.

      Can I see the actual data?

      update: FWIW those errors might go away if you parse with

      Text::CSV_XS->new ({ binary => 1, allow_loose_quotes => 1 });

      or

      Text::CSV_XS->new ({ binary => 1, escape_char => undef });

      Enjoy, Have FUN! H.Merijn

        that is magic for me. Where did you get this man page from.

        Great that you want to have a look at it. The file is created by a Filemaker DB (fmpro 6 --> Export CSV: output file character set: Macintosh). Would it be possible to e-mail you this? It is not that big, less then 500kb

        If I would just cut and paste it'll probably all work just fine as with opening and saving the script works fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://799461]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found