Text::CSV_XS, parse(), fields(), error_input()

来源:互联网 发布:重庆网络优化公司 编辑:程序博客网 时间:2024/05/16 23:10

如果field里面也包含分隔符(比如"tom,jack,jeff","rosemike",O'neil,"kurt,korn"),那么我们解析起来确实有点麻烦,Text::CSV_XS挺方便。

#!/usr/bin/perl use strict; use Text::CSV_XS; my @columns; my $csv = Text::CSV_XS->new({                      'binary' => 1,                      'quote_char'  => '"',                          'sep_char'    => ','                            });  foreach my $line(<DATA>) {    chomp $line;    if($csv->parse($line))    {       @columns = $csv->fields();    }    else    {       print "[error line : ", $csv->error_input, "]\n";    }    map {printf("%-14s\t", $_)} @columns;    print "\n"; } exit 0;

__DATA__

id,compact_sn,name,type,count,price

37,"ITO-2003-011","台式机,compaq","128M","290","1,2900"

35,I-BJ-2003-010,"显示器,硬盘,内存",'三星',480,"1,4800"

55,"C2003-104",笔记本,"Dell,Latitude,X200",13900,"1,13900"


0 0
原创粉丝点击