Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Code Misses a Replacement

by Xaositect (Friar)
on Jul 07, 2005 at 19:48 UTC ( #473210=note: print w/ replies, xml ) Need Help??


in reply to Code Misses a Replacement

This may makes things more complicated than you need, but I should point out that most CSV dumps use quotation marks to escape strings that have commas in them. This is something to watch for: some,comma-delniated,"file with a, comma",in the data

You might take a look at Text::CSV, you could do something like: (untested)

use Text::CSV; my $csv = Text::CSV->new(); while (<>) { $csv->parse($_); print join("\t", $csv->fields()); }
That's pretty simplistic, and won't handle tabs in the data, but you get the idea.


Xaositect - Whitepages.com


Comment on Re: Code Misses a Replacement
Select or Download Code
Re^2: Code Misses a Replacement
by radiantmatrix (Parson) on Jul 07, 2005 at 22:17 UTC

    I would agree that the dump may do many interesting things when it goes to CSV, including escaping certain chars. I suggest the following code (which I use a variation of to convert Semi-Colon SV files to CSV files):

    use IO::File; use Text::CSV_XS; for (@ARGV) { my $out_fname = $_.'.dshield'; my $inf = new IO::File ( $_,'<' ) or die "Cannot read $_"; my $outf = new IO::File ( $out_fname ,'>' ) or die "Cannot write $o +ut_fname"; my $csv_in = new Text::CSV_XS; # defaults work for most CSV's my $csv_out = new Text::CSV_XS({sep_char=>"\t"}); # use tabs until ($inf->eof) { my $line = $csv_in->getline($inf); $csv_out->print($outf, $line); } } ## IO::File objects close automatically when they go out of scope

    This gets used as:

    c2t.pl file1.out {file2.out} {...}
    , and writes the results to file1.out.dshield, etc. By using the Text::CSV_XS module, you will be certain of processing CSV and Tab-SV files correctly. Though it's more code, it performs quite well and it will likely save you grief in the future.
    Larry Wall is Yoda: there is no try{}
    The Code that can be seen is not the true Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (14)
As of 2015-07-07 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls