Text ::当数据包含换行符时,CSV解析 [英] Text::CSV parsing when data contains newline

查看:214
本文介绍了Text ::当数据包含换行符时,CSV解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码来解析csv文件,数据也包含换行符。
但是Text :: CSV在数据中遇到\\\
时中断

I have a code that parses a csv file and the data also contains newline. But Text::CSV breaks when it encounters "\n" inside the data

这是解析代码

use Data::Dumper;
use Text::CSV;
my $csv = Text::CSV->new ({ binary=> 1, eol => $/, allow_loose_quotes => 1, allow_loose_escapes=> 1 }) || die $!;
#print Dumper($csv);                                                                                                                           

my $file = $ARGV[0];
open my $csv_handle,  $file  or die $!;
while (my $row = $csv->getline($csv_handle)) {
    print Dumper($row);
}

这是数据

196766,31,"MR SRINIVASALU LAKSHMIPATHY\"DEC\"\
\"71"
196766,56,"255233.47"


推荐答案

您还需要设置 escape_char \ ,因为它默认为。但是,如果您运行pure-perl版本的 Text :: CSV ,这不会解决问题。使用XS版本( Text :: CSV_XS ):

You also need to set the escape_char to \, as it defaults to ". However, this doesn't fix the problem if you run the pure-perl version of Text::CSV. With the XS version (Text::CSV_XS), this works:

use strict; use warnings;
use Text::CSV;
use Data::Dumper;

my $csv = Text::CSV->new({
    binary => 1,
    eol => "\n",
    quote_char => '"',
    escape_char => '\\',
    auto_diag => 2,
    allow_loose_escapes => 1,
}) or die "Can't create CSV parser";

while( my $row = $csv->getline(\*DATA) ) {
    print Dumper $row;
}

__DATA__
1,"2
",3
196766,31,"MR SRINIVASALU LAKSHMIPATHY\"DEC\"\
\"71"
196766,56,"255233.47"

纯Perl解析器在第二个记录上失败,并抱怨缺少结束报价。如果我们将 allow_loose_quotes 设置为真值,那么CSV解析,但第二个记录被拆分(第三个记录包含 \\71)。 XS版本不会显示此行为。

The pure-Perl parser fails on the 2nd record and complains about a missing closing quote. If we set allow_loose_quotes to a true value, then the CSV parses, but the 2nd record is split apart (a third record with a sole field containing \"71" is inserted). The XS version does not show this behaviour.

这看起来像是Text :: CSV_PP中的错误。

This looks like a bug in Text::CSV_PP.

这篇关于Text ::当数据包含换行符时,CSV解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆