CSV_XS Question

J

jeffg

I'm trying to use CSV_XS to parse log files.
Each field is separated by a comma and surround by double quotes.

Within each field, there may be embdedded double and single quotes.
These embedded quotes are escaped by a backslash.

I'm having trouble getting it to work. I have two questions:

1) The entire script fails at the first line it can't parse. How can
I make it ignore the current line and continue?
I'm using getline() to read the file, like this:

open my $io, "<", $logname or warn("Cannot open file ${logname} for
reading. $!");
while (my $line = $csv->getline($io)) {

}

2) How should I instantiate my $csv object to get it to recognize my
escape character?

I tried this:

my $csv = Text::CSV_XS->new( {
escape_char => '\\',
sep_char => ',',
binary => 1,
} );
 
B

Ben Morrow

Quoth (e-mail address removed):
I'm trying to use CSV_XS to parse log files.
Each field is separated by a comma and surround by double quotes.

Within each field, there may be embdedded double and single quotes.
These embedded quotes are escaped by a backslash.

I'm having trouble getting it to work. I have two questions:

1) The entire script fails at the first line it can't parse. How can
I make it ignore the current line and continue?
I'm using getline() to read the file, like this:

open my $io, "<", $logname or warn("Cannot open file ${logname} for
reading. $!");

You want to die at this point, not warn. There's no point continuing if
the open failed: $io is undef, and the rest of the program will just
give unhelpful errors.
while (my $line = $csv->getline($io)) {

}

->getline returns false on EOF or on parse error. You can tell the
difference with ->eof; something like

CSV:
while (my $line = $csv->getline($io)) {
...
}

unless ($csv->eof) {
warn "bad line in CSV file\n";
redo CSV;
}

Alternatively you could read the lines in yourself and call ->parse on
them; that way you can show the user what the bad line looked like. Be
careful to make sure you get complete records, if you do this.
2) How should I instantiate my $csv object to get it to recognize my
escape character?

I tried this:

my $csv = Text::CSV_XS->new( {
escape_char => '\\',
sep_char => ',',
binary => 1,
} );

It looks to me as though you need the allow_loose_escapes option, to
allow you to escape ' when it doesn't really need it.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top