gpt4 book ai didi

perl - CSV 数据中的换行符 Text::CSV reading

转载 作者:行者123 更新时间:2023-11-30 23:47:24 27 4
gpt4 key购买 nike

我有一些示例数据(来自 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 作为换行符。有没有办法解决?

最佳答案

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

Important Note: The default behavior is to accept only ASCII characters in the range from 0x20 (space) to 0x7E (tilde). This means that the fields can not contain newlines. If your data contains newlines embedded in fields, or characters above 0x7E (tilde), or binary data, you must set binary => 1 in the call to new. To cover the widest range of parsing options, you will always want to set binary.

关于perl - CSV 数据中的换行符 Text::CSV reading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48771118/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com