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

Re: Writing CSV files

by steves (Curate)
on Oct 29, 2004 at 09:50 UTC ( #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.


Comment on Re: Writing CSV files
Select or Download Code
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
Node Status?
node history
Node Type: note [id://403662]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-07-24 18:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (165 votes), past polls