Skip Menu |

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

Report information
The Basics
Id: 99941
Status: resolved
Priority: 0/
Queue: Text-CSV_XS

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

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: 1.12



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
Subject: Re: [rt.cpan.org #99941] Feature Request: Ability to retain quotes on CSV fields that were quoted in the input
Date: Sat, 1 Nov 2014 14:41:04 +0100
To: bug-Text-CSV_XS [...] rt.cpan.org
From: "H.Merijn Brand" <h.m.brand [...] xs4all.nl>
On Fri, 31 Oct 2014 19:12:34 -0400, "Brian Paterni via RT" <bug-Text-CSV_XS@rt.cpan.org> wrote: Show quoted text
> 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 problem with this feature request is that meta_info is kept per record and not per file. If you read the whole CSV stream, this new feature will only cause confusion. If however you will read one record at the time and write that record before reading the new record. It will use some same default for eol. my $csv = Text::CSV_XS->new ({ keep_meta_info => 1 }); open my $fi, "<", "in.csv"; open my $fo, "<", "out.csv"; while (my $row = $csv->getline ($fi)) { $row->[7] =~ s/old/new/; $csv->print ($fo, $row); } would that be an acceptable solution? (NYI!) -- H.Merijn Brand http://tux.nl Perl Monger http://amsterdam.pm.org/ using perl5.00307 .. 5.21 porting perl5 on HP-UX, AIX, and openSUSE http://mirrors.develooper.com/hpux/ http://www.test-smoke.org/ http://qa.perl.org http://www.goldmark.org/jeff/stupid-disclaimers/
Download (untitled)
application/pgp-signature 490b

Message body not shown because it is not plain text.