CSV数据中的换行符会破坏Text :: CSV读取

时间:2018-02-13 16:16:04

标签: perl

我有一些示例数据(来自 Google表格电子表格);

Title,Website URL,Description,Contact Number,Address 1,Address 2,City,State ,Province,Country,Facebook,Twitter,Full Category Name
Sample title,http://www.test.com,"A short description. Press Ctrl+Enter to create a new line

Like this",44 (0)1430 123 123,Some address line,Line 2,The city,The state,Some province,UK,facebook.com/test,twitter.com/test,Full Category/Path/To/Category or ID

然后我有了这个简单的脚本:

use Text::CSV;
my $csv = Text::CSV->new({
    sep_char => ','
   });
   $csv->column_names("title","url","description","contact_number","address1","address2","city","state","province","country","facebook","twitter","category_name");

my $file = './tmp/$USER->{Username}.csv';

open (WRITEIT, ">:encoding(utf8)", $file) or die "cant write $file: $!";
    print WRITEIT join("\n", @data). "\n";
close (WRITEIT) or die "cant write $file: $!";

open my $io, "<:encoding(utf8)", $file or die "$file: $!";

use Data::Dumper;

my $i = 0;
while (my $row = $csv->getline_hr($io)) {

    $i++;

    print Dumper($row);

}

问题在于,我得到以下输出:

$VAR1 = 'Sample title,http://www.test.com,"A short description. Press Ctrl+Enter to create a new line
';
$VAR2 = '
';
$VAR3 = 'Like this",44 (0)1430 123 123,Some address line,Line 2,The city,The state,Some province,UK,facebook.com/test,twitter.com/test,Full Category/Path/To/Category or ID';

它将描述中的\ n作为换行符。有没有办法解决?

1 个答案:

答案 0 :(得分:8)

https://metacpan.org/pod/Text::CSV#Embedded-newlines

  

重要说明:默认行为是仅接受0x20(空格)到0x7E(代字号)范围内的ASCII字符。这意味着字段不能包含换行符。如果您的数据包含字段中嵌入的换行符,或0x7E(代字号)或二进制数据上方的字符,则 必须 在通话中设置binary => 1new。要覆盖最广泛的解析选项,您始终需要设置二进制文件。