Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: CSV with embedded newlines

by kal (Hermit)
on Apr 04, 2003 at 08:24 UTC ( [id://247997]=note: print w/replies, xml ) Need Help??


in reply to CSV with embedded newlines

I'm pretty sure that Tie::CSV gets this wrong, unfortunately - I've tried to use it in the past, and never had much success. However, Text::CSV has worked for me with this type of file (well, Text::CSV_XS, which should be the same thing ;)

The basic loop structure that I have is something like:

my $csv = Text::CSV_XS->new({'binary' => 1}); my $current_line; while (<CVSFILE>) { $current_line .= $_; next unless ($csv->parse ($current)); my @row = $csv->fields(); $current = ''; # do stuff with the rows... }

This seemed to hold up against some really bizarre files (very _very_ big files, for example), so it looks pretty good. Strangely, I think Tie::CSV uses the Text::CSV module to read lines in, but I don't think it gets the loop right (like above). However, this might be my memory playing tricks.

Replies are listed 'Best First'.
Re: Re: CSV with embedded newlines
by doom (Deacon) on Apr 04, 2003 at 11:07 UTC
    I'm pretty sure that kal is right here, the key feature being the use of the "binary" option. One of the many silly things about CSV_XS is that you *always* want to use the "binary" option (this is reminiscent of FTP in the old days, before they got a clue and made "binary" the default). Pretty much all real text is "binary" from the point of view of CSV_XS (e.g. if you want to use any iso8859 extended characters).

    But the last time I looked you definitely wanted to use "CSV_XS", not the older "CSV" module. They're really not the same.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found