in reply to Re: Extracting fields from CSV
in thread Extracting fields from CSV

If you are able to connect to the database from perl, using DBI, you don't need an intermediate file at all.

Use Text::CSV_XS (or Text::CSV) directly to generate the output and it will correctly deal with the quoting:

use strict; use Text::CSV_XS; my $csv_in = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, sep_char => "|", allow_whitespace = +> 1 }); my $csv_out = Text::CSV_XS->new ({ binary => 1, eol => "\n" }); my $infile = "s.txt"; my $outfile = "s.csv"; open my $fh, "<", $infile or die "$infile: $!"; open my $out, ">", $outfile or die "$outfile: $!"; while (my $row = $csv_in->getline ($fh)) { $csv_out->print ($out, $row); }

update: corrected $fh to $out in while loop.


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^3: Extracting fields from CSV
by suhailck (Friar) on Aug 31, 2010 at 07:05 UTC
    Thanks. This is what i really wanted :)

    There is a small typo in the above code

    $csv_out->print ($fh, $row);
    Filehandle $out is needed in place of $fh for writing

    $csv_out->print ($out, $row);

    ~suhail