Subject: | Feature Request: Ability to retain quotes on CSV fields that were quoted in the input |
Date: | Fri, 31 Oct 2014 18:12:12 -0500 |
To: | bug-Text-CSV_XS [...] rt.cpan.org |
From: | Brian Paterni <bpaterni [...] gmail.com> |
Hi,
I filed a bug at Text-CSV with the same name:
https://rt.cpan.org/Public/Bug/Display.html?id=99821
but I'm wondering if I should have reported it here since Text-CSV acts
as an interface for either CSV_XS or CSV_PP.
The following is the text from Bug ID 99821:
I have a CSV file such that a few of the fields are quoted regardless of
whether they need to be. What I wish to do is load this file, modify a
few values, and produce the modified CSV with the quoted fields matching
the input.
The following test script should demonstrate the problem:
use Text::CSV;
my $csv = Text::CSV->new ({'binary' => 1, 'allow_loose_quotes' => 1,
'keep_meta_info' => 1});
my $line = q^hello,"world"^;
print qq^input: $line\n^;
$csv->parse($line);
my @flds = $csv->fields();
$csv->combine(@flds);
print 'output: ', $csv->string(), "\n";
produces:
input: hello,"world"
output: hello,world
I've found that Text::CSV does offer meta_data and that the is_quoted
function may be used to determine if a given input field was quoted or
not. However, when I use this capability to add quotes myself, I'm met
with undesirable results. Text::CSV's combine() subroutine sees these
added quotes as part of the field data and will escape them:
my $csv = Text::CSV->new ({'binary' => 1, 'allow_loose_quotes' => 1,
'keep_meta_info' => 1});
my $line = q^hello,"world"^;
print qq^input: $line\n^;
$csv->parse($line);
my @flds = $csv->fields();
for my $idx (0..$#flds) {
if ($csv->is_quoted($idx)) {
$flds[$idx] = qq^"$flds[$idx]"^;
}
}
$csv->combine(@flds);
print 'output: ', $csv->string(), "\n";
Producing:
input: hello,"world"
output: hello,"""world"""
Hopefully adding functionality to retain quotes in combine() is not too
difficult given the Text::CSV modules already track field metadata.
Unfortunately though, I'm not too privy on the internals of the module
to really dive into this.
NOTE: report based heavily on my earlier question submitted to
stackoverflow:
http://stackoverflow.com/questions/26569459/retain-quotes-on-csv-fields-that-were-quoted-in-the-input