Reputation: 966
I have written a simple perl script to read a line from a .csv file. The code is as per below:
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new({ binary => 1 });
open my $fh, "<", "testresults.csv" or die "testresults.csv $!";
while ( my $row = $csv->getline( $fh ) ) {
my @fields = @$row;
}
$csv->eof or $csv->error_diag;
close $fh or die "testresults.csv $!";
And the testresults.csv file looks like this:
Node1,Node2,Node3,Node4,map1,map2,map3,map4,map5,map6,map7,map8,DM,LAT,AVG,product
on the first line followed by the results on each line:
name1,name2,name3,name4,Node1,Node2,Node3,Node4,Node5,Node6,Node7,Node8,0%,
0.002835480002,0.1714008533,4.86003691857886E-04
and so on.
I am getting the following error with my code when I do a ./filename.pl from the command prompt: CSV_XS ERROR: 2032 - EIF - CR char inside unquoted, not part of EOL @ pos 420
I tried to google for this error but could not fathom much into this error.
Upvotes: 2
Views: 2408
Reputation: 67930
It would seem from our conversation in the comments that the error comes of the strings in the input being interlaced with null characters, being made visible by using
use Data::Dumper;
$Data::Dumper::Useqq = 1;
while (<$fh>) {
print Dumper $_;
}
A quick hack is to strip the null characters in the input file with something like:
perl -i.bak -pwe 'tr/\0//d' testresults.csv
NOTE: But as has been pointed out in comments from people more experienced in encoding matters, this can/could/should be solved by decoding your data instead. Just stripping the bad symbols might break your data in subtle ways, and is not an ideal solution.
I'm sorry, I do not know much about that, but using Text::CSV::Encoded does sound like a good start, like cjm suggested.
Upvotes: 3