使用Perl Text::CSV解析包含特殊字符的字段

5

我正在使用Text::CSV模块从一个以制表符分隔的值文件中解析行为各种字段。

字符串中特殊字符的例子:

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

我的代码如下:

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;

上面是我代码的一些重要部分。我得到的错误如下:
cvs_xs error : 2026 - EIQ - Binary Character inside quoted field, binary off @pos 15

6
重要提示:默认行为是只接受ASCII字符。这意味着字段不能包含换行符。如果您的数据包含嵌入在字段中的换行符,或者超出0x7e(波浪号)的字符或二进制数据,则必须在调用new()时设置binary => 1。为了涵盖最广泛的解析选项,您始终需要设置binary。 - alex
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接