Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing fields with special characters using Perl Text::CSV

Tags:

perl

I am using the Text::CSV module to parse lines into various fields from a tab-separated value file.

Examples of special characters in strings are

"CEZARY Å?UKASZEWICZ, PAWEÅ? WIETESKA","BÜRO FÜR"

My code goes as below:

my $file = $ARGV[0] or die "Need to get TSV file on the command line\n";

my $csv = Text::CSV->new({sep_char => "\t"});

open(my $data,'<', $file) or die "Could not open '$file' $!\n";


while (my $line= <$data>) {

       if($csv->parse($line)){
            my @curr_arr = $csv->fields();

        }
} # end of while

close $data;

The above is some of the important parts of my code. The error I get is as follows:

cvs_xs error : 2026 - EIQ - Binary Character inside quoted field, binary off @pos 15
like image 989
Sanjay Rao Avatar asked Jan 09 '13 22:01

Sanjay Rao


People also ask

How do I read a column from a CSV file in Perl?

To read a column from csv for this purpose I wrote this script: #!/usr/bin/perl -w use strict; use warnings; use Text::CSV; my$column_separator = qr/,/; my $column_number = "3"; my$file = "/home/Admin/Documents/new (copy).

What does parsing csv mean?

Parsing a file means reading the data from a file. The file may contain textual data so-called text files, or they may be a spreadsheet.


1 Answers

my $csv = Text::CSV->new({ binary => 1, sep_char => "\t"});
like image 69
alex Avatar answered Oct 16 '22 14:10

alex