我在安装了Text::CSV
的Windows 8.1 64位上使用Strawberry Perl 5.18.2.2,并且
我试图从PayPal解析以下CSV文件。
Date, Time, Time Zone, Name, Type, Status, Gross, Fee, Net, From Email Address, To Email Address, Transaction ID, Counterparty Status, Address Status, Item Title, Item ID, Shipping and Handling Amount, Insurance Amount, Sales Tax, Option 1 Name, Option 1 Value, Option 2 Name, Option 2 Value, Auction Site, Buyer ID, Item URL, Closing Date, Escrow Id, Invoice Id, Reference Txn ID, Invoice Number, Custom Number, Receipt ID, Balance, Address Line 1, Address Line 2/District/Neighborhood, Town/City, State/Province/Region/County/Territory/Prefecture/Republic, Zip/Postal Code, Country, Contact Phone Number,
"5/5/2014","17:44:45","PDT","Jack Payer","Payment Received","Completed","1.00","0.00","1.00","jack@yahoo.com","payer@gmail.com","8HT05934290026J","Verified","","","","","","","","","","","","","","","","","","","","","1.67","","","","","","","",
使用以下命令打开,读取和解析文件:
open(READ, $sourcefile)
open($fh,"+>:encoding(utf8)","$base.m.csv")
undef $/;
$_ = <READ>;
# convert Unix line ending to dos
$_ =~ s/\r?\n|\r/\r\n/g;
print $fh $_;
close READ;
$/ = "\r\n";
seek($fh, 0, 0);
my $csv = Text::CSV->new({ allow_whitespace => 1, binary => 1 }); # should set binary attribute.
my $row=$csv->getline ($fh);
my @fields = @$row;
$csv->column_names (@fields);
$row = $csv->getline_hr($fh)
这是问题,从Gross列开始,它读错了,然后弄乱了所有后续列。 $row->{'Gross'}
读取1.00,^@.00
而不是1.00
。
^@
符号是我用gvim打开输出文件时看到的特殊NULL
字符。这就是问题的开始。
随后,$gross=$row->{'Fee'}
读取1.00
而不是0.00
,$gross=$row->{'Net'}
读取jack@yahoo.com
而不是1.00
知道它为什么搞砸了?
编辑:此处要求的完整代码: https://www.dropbox.com/s/p064pxitmw3jwmj/csv2qif.pl
编辑:如果我改变
"1.00","0.00","1.00"
CSV文件中的
"1.00","2.00","3.00"
它工作正常。事实上任何以&#34; 0.xx&#34;开头的东西它最终将其解析为NULL字符。 我没有得到NULL字符。我迷路了,CSV解析器出了什么问题?
答案 0 :(得分:0)
感谢@ThisSuitIsBlack,问题不在于Text :: CSV。引用
我在v1.12(2009年5月)下的Text :: CSV更改日志中发现了这一点:getline()没有处理具有null的行(例如“0”)。你是否正在使用旧版本?请查看perl -MText :: CSV -e'print $ Text :: CSV :: VERSION'
问题是Strawberry Perl和Text :: CSV的结合。我升级到32位5.20.0.1便携版并重新安装了Text :: CSV版本1.32(此时最新版本),它开始正常工作。