Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Text-CSV CPAN distribution.

Report information
The Basics
Id:
99821
Status:
resolved
Priority:
Low/Low
Queue:

People
Owner:
Nobody in particular
Requestors:
bpaterni [...] gmail.com
Cc:
AdminCc:

BugTracker
Severity:
(no value)
Broken in:
(no value)
Fixed in:
(no value)



Subject: Feature Request: Ability to retain quotes on CSV fields that were quoted in the input
Date: Sun, 26 Oct 2014 13:10:21 -0500
To: bug-Text-CSV@rt.cpan.org
From: Brian Paterni <bpaterni@gmail.com>
Hello, 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 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. Though unfortunately, 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
On Mon Oct 27 03:10:41 2014, bpaterni@gmail.com wrote:
Show quoted text
> Hello, > > 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 > 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. > Though unfortunately, 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
Hi. With the latest Text::CSV/CSV_XS/CSV_PP, set keep_meta_info to 10 or higher and I think you'll get what you want. See https://metacpan.org/pod/Text::CSV_XS#keep_meta_info for more details.
Closed. Thanks.


This service runs on Request Tracker, is sponsored by The Perl Foundation, and maintained by Best Practical Solutions.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.