|
From: jeffg on 14 Apr 2008 16:31 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, } );
From: Ben Morrow on 14 Apr 2008 16:49 Quoth jeffg(a)programmer.net: > 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
|
Pages: 1 Prev: Textview not scrolling to show full text in IRC client Next: CSV to quasi-XML |