RG925
RG925

Reputation: 23

Using Text::CSV on a String Containing Quotes

I have pored over this site (and others) trying to glean the answer for this but have been unsuccessful.

use Text::CSV;

my $csv = Text::CSV->new ( { binary => 1, auto_diag => 1 } );

$line = q(data="a=1,b=2",c=3);

my $csvParse  = $csv->parse($line);

my @fields = $csv->fields();

for my $field (@fields) {
    print "FIELD ==> $field\n";
}

Here's the output:

# CSV_XS ERROR: 2034 - EIF - Loose unescaped quote @ rec 0 pos 6 field 1
FIELD ==> 

I am expecting 2 array elements:

data="a=1,b=2"
c=3

What am I missing?

Upvotes: 0

Views: 554

Answers (2)

TLP
TLP

Reputation: 67940

You may get away with using Text::ParseWords. Since you are not using real csv, it may be fine. Example:

use strict;
use warnings;
use Data::Dumper;
use Text::ParseWords;

my $line = q(data="a=1,b=2",c=3);
my @fields = quotewords(',', 1, $line);
print Dumper \@fields;

This will print

$VAR1 = [
          'data="a=1,b=2"',
          'c=3'
        ];

As you requested. You may want to test further on your data.

Upvotes: 3

hobbs
hobbs

Reputation: 240829

Your input data isn't "standard" CSV, at least not the kind that Text::CSV expects and not the kind that things like Excel produce. An entire field has to be quoted or not at all. The "standard" encoding of that would be "data=""a=1,b=2""",c=3 (which you can see by asking Text::CSV to print your expected data using say).

If you pass the allow_loose_quotes option to the Text::CSV constructor, it won't error on your input, but it won't consider the quotes to be "protecting" the comma, so you will get three fields, namely data="a=1, b=2" and c=3.

Upvotes: 2

Related Questions