如何处理带有 CRLF、NEL 行终止符的行?

发布于 2024-08-16 23:58:52 字数 418 浏览 2 评论 0原文

我需要使用 shift_jis 编码处理文件。然而,行终止符的格式我不熟悉。

> file record.CSV 
record.CSV: Non-ISO extended-ASCII text, with CRLF, NEL line terminators

我使用一般:

open my $CSV_FILE, "<:encoding(shift_jis)", $filename or die "Could not open: $CSV_FILE : $!";
while (<$CSV_FILE>) {
    chomp;
    # do stuff
}

但是它仍然在每条记录的末尾留下一个 CR。

终止这些类型的文件的正确方法是什么?

I need to process a file with shift_jis encoding. However the line terminators are in a format that im not familar with.

> file record.CSV 
record.CSV: Non-ISO extended-ASCII text, with CRLF, NEL line terminators

Im using the general:

open my $CSV_FILE, "<:encoding(shift_jis)", $filename or die "Could not open: $CSV_FILE : $!";
while (<$CSV_FILE>) {
    chomp;
    # do stuff
}

However it is still leaving a CR at the end of each record.

What is the correct way to terminate files of these types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2024-08-23 23:58:52

为什么不手动执行 $_ =~ s/\r//

编辑:显然,您还可以

require Encode;
use Unicode::Normalize;

s/\x{0085}//g;

删除 NEL: Next Line, U+0085 字符。

Why not do $_ =~ s/\r// manually?

Edit: apparently, you can also do

require Encode;
use Unicode::Normalize;

s/\x{0085}//g;

to remove the NEL: Next Line, U+0085 characters.

撩心不撩汉 2024-08-23 23:58:52

您需要考虑谁在使用数据并了解有关生成这些文件的环境的更多信息。如果您最终想要的是一个普通的 CSV 输出文件,请使用您喜欢的任何旧字符串操作来删除它们(并生成 CRLF 终止符来代替它们),然后就可以了。

You need to consider who's consuming the data and learn more about the environment which produced these files. If it's a plain-vanilla CSV output file you're after in the end, use any old string manipulation you like to get rid of them (and produce CRLF terminators in their stead) and you'll be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文