in reply to Creating .csv file with comma

Hi,

Your values passed to Text::CSV are:

123456 Prototype "Number in address is 100. Please, check your records."
... so Text::CSV escapes the value that has spaces (the default setting) using the default escape character, which is ... a double quote mark.

But it's only happening because of how you are assembling the value (use sprintf) or because you are trying to quote the comma. Just let Text::CSV handle it:

$ perl -Mstrict -MText::CSV=csv -wE 'my $num = 100; csv(in => [["12345 +65", "Prototype", "Number in address is $num. Please, Please check yo +ur records.", "foo bar", "baz,qux"]], out => *STDOUT)' 1234565,Prototype,"Number in address is 100. Please, Please check your + records.","foo bar","baz,qux"
Note 'foo bar' quoted because it contains a space; 'baz,qux' quoted because it contains the field separator character.

Change the separator character:

$ perl -Mstrict -MText::CSV=csv -wE 'my $num = 100; csv(in => [["12345 +65", "Prototype", "Number in address is $num. Please, Please check yo +ur records.", "foo bar", "baz,qux"]], out => *STDOUT, sep_char => "|" +)' 1234565|Prototype|"Number in address is 100. Please, Please check your + records."|"foo bar"|baz,qux
Note 'baz,qux' not quoted because does not contain the separator character, 'but 'foo bar' and your string still quoted because they contain spaces.

Also turn off quoting of values with spaces:

$ perl -Mstrict -MText::CSV=csv -wE 'my $num = 100; csv(in => [["12345 +65", "Prototype", "Number in address is $num. Please, Please check yo +ur records.", "foo bar", "baz,qux"]], out => *STDOUT, sep_char => "|" +, quote_space => 0)' 1234565|Prototype|Number in address is 100. Please, Please check your +records.|foo bar|baz,qux
Note no quoting even where values have spaces (not recommended).

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Creating .csv file with comma
by Anonymous Monk on Apr 24, 2020 at 14:29 UTC
    This is a sample on how I am formatting the file:
    #!/usr/bin/perl use strict; use warnings; my $number = "100"; my @data; push @data, [ '1234565', "Prototype", '"Number in address is '.$number.'. Please, Please check + your records."', ]; my $csv = Text::CSV->new ( { binary => 1, eol => "\n", quote_char => +'', } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, >, "myfile.csv" or die "Unable to create file: $!"; $csv->print($fh, $_) for @{ $data }; close $fh;

    Thank you!

      Hi again,

      sprintf -- essential tool for avoiding hackery with interpolation and escaping.

      push(@data, [ 1234565, 'Prototype', sprintf('Number in address is %s. Please, Please check your record +s.', $number), ]);
      (I advise always using parens with push and any function that will slurp up whatever's left on the line as args...)

      Hope this helps!


      The way forward always starts with a minimal test.
      I needed to fix the code to make it work and show the issue with double quotes twice.
      #!/usr/bin/perl use strict; use warnings; use Text::CSV; my $number = "100"; my @data = (); push @data, [ '1234565', "Prototype", '"Number in address is '.$number.'. Please, Please check + your records."', ]; my $data = \@data; my $csv = Text::CSV->new ( { binary => 1, eol => "\n", quote_char => +'', } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, ">", "myfile.csv" or die "Unable to create file: $!"; $csv->print($fh, $_) for @{ $data }; close $fh;

      Sorry for that! Thanks!

        As I showed, remove the quote_char => '' option and remove the double quotes from inside the string '"Number in address is '.$number.'. Please, Please check your records."'.

        So why do you put "1234565" and 'Prototype' in one pair of quotes each, and the third element in two pairs of quotes?

        You don't need an extra pair of quotes for the comma, that is Text::CSV's Task