in reply to CSV file with double quotes

use strict; use warnings; use Text::CSV_XS; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "\n +" }); open my $fh, "<:encoding(utf-8)", "file.csv" or die "file.csv: $!"; while (my $row = $csv->getline ($fh)) { s/,/~z~/g for @$row; $csv->print (*STDOUT, $row); }

Simple enough?


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: CSV file with double quotes
by rahulr (Initiate) on Sep 14, 2011 at 14:28 UTC
    Yes... looks jolly simple.
    All I need is, get the module installed and I am ready to go.
    Just curious, which bit here is differentiating between those commas which are delimiters and those commas which are data (i.e. within the double quotes)? I cannot try it right now coz the module is not installed

      The snippet my $row = $csv->getline ($fh) in the example reads a line from the source file, and intelligently splits it up into fields. The Text::CSV library knows how do do that and takes quotes and the like into account. The $row scalar that it returns is slightly misleading as it is actually an array reference to the fields it extracted.

      The next line: s/,/~z~/g for @$row; iterates over each field in the row and performs your substitution.

      Question to rahulr: Why do you want to replace commas with "~z~"? It looks like you are trying to escape them. If so what would you do if you encounter a real "~z~" in your input? You might want to re-think how you are processing your data so that escaping works properly.