in reply to Creating .csv file with comma
Hi,
Your values passed to Text::CSV are:
... so Text::CSV escapes the value that has spaces (the default setting) using the default escape character, which is ... a double quote mark.123456 Prototype "Number in address is 100. Please, check your records."
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:
Note 'foo bar' quoted because it contains a space; 'baz,qux' quoted because it contains the field 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)' 1234565,Prototype,"Number in address is 100. Please, Please check your + records.","foo bar","baz,qux"
Change the separator character:
Note 'baz,qux' not quoted because does not contain the separator character, 'but 'foo bar' and your string still quoted because they contain 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 => "|" +)' 1234565|Prototype|"Number in address is 100. Please, Please check your + records."|"foo bar"|baz,qux
Also turn off quoting of values with spaces:
Note no quoting even where values have spaces (not recommended).$ 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
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Creating .csv file with comma
by Anonymous Monk on Apr 24, 2020 at 14:29 UTC | |
by 1nickt (Canon) on Apr 24, 2020 at 19:33 UTC | |
by Anonymous Monk on Apr 24, 2020 at 14:51 UTC | |
by haukex (Archbishop) on Apr 24, 2020 at 17:29 UTC | |
by Anonymous Monk on Apr 24, 2020 at 19:07 UTC | |
by soonix (Chancellor) on Apr 24, 2020 at 15:40 UTC |