Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Writing CSV files

by steves (Curate)
on Oct 29, 2004 at 09:50 UTC ( [id://403662]=note: print w/replies, xml ) Need Help??


in reply to Writing CSV files

The default behavior for Text::CSV_XS works as you want things to. Example:

use strict; use Text::CSV_XS; use IO::File; my $csv = Text::CSV_XS->new(); my @row; while (<DATA>) { chomp; @row = split(/\|/, $_); $csv->print(\*STDOUT, \@row); print "\n"; } __DATA__ Field1|Field Number Two|Field Number Three|Field4 Another One|foo|fubar|foo and fubar
produces:
Field1,"Field Number Two","Field Number Three",Field4 "Another One",foo,fubar,"foo and fubar"

I've found Text::CSV_XS to be the most accurate at generating true CSV, versus just delimited by commas, which some people seem to think is CSV.

Replies are listed 'Best First'.
Re^2: Writing CSV files
by tachyon (Chancellor) on Oct 29, 2004 at 12:03 UTC

    The default behavior for Text::CSV_XS works as you want things to.

    Actually what you have shown and what Text::CSV_XS does has no relationship whatsoever to what the OP wanted.

      You are correct tachyon. It took me three cups of coffee to see that. My apologies. The issue here is rather custom since none of the CSV packages I've used will tell you if something was quoted coming in -- they hide that. It seems like to do this right you need to know what was quoted coming in and mirror that on the way out. If the rules are based on content (e.g., something that can be regexp matched) then maybe you don't need to know how it was originally.

Log In?
Username:
Password:

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

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

    No recent polls found