perl - Text::CSV parsing when data contains newline -


i have code parses csv file , data contains newline. text::csv breaks when encounters "\n" inside data

this parse code

use data::dumper; use text::csv; $csv = text::csv->new ({ binary=> 1, eol => $/, allow_loose_quotes => 1, allow_loose_escapes=> 1 }) || die $!; #print dumper($csv);                                                                                                                             $file = $argv[0]; open $csv_handle,  $file  or die $!; while (my $row = $csv->getline($csv_handle)) {     print dumper($row); } 

this data

196766,31,"mr srinivasalu lakshmipathy\"dec\"\ \"71" 196766,56,"255233.47" 

you need set escape_char \, defaults ". however, doesn't fix problem if run pure-perl version of text::csv. xs version (text::csv_xs), works:

use strict; use warnings; use text::csv; use data::dumper;  $csv = text::csv->new({     binary => 1,     eol => "\n",     quote_char => '"',     escape_char => '\\',     auto_diag => 2,     allow_loose_escapes => 1, }) or die "can't create csv parser";  while( $row = $csv->getline(\*data) ) {     print dumper $row; }  __data__ 1,"2 ",3 196766,31,"mr srinivasalu lakshmipathy\"dec\"\ \"71" 196766,56,"255233.47" 

the pure-perl parser fails on 2nd record , complains missing closing quote. if set allow_loose_quotes true value, csv parses, 2nd record split apart (a third record sole field containing \"71" inserted). xs version not show behaviour.

this looks bug in text::csv_pp.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -