perl 输出 - 无法正确打印 utf8 文本文件
perl output - failing in printing utf8 text files correctly
所以我有 utf8 文本文件,我想读入这些文件,将这些行放入一个数组中,然后打印出来。但是输出却没有正确打印符号,例如输出行如下所示:
"arnſtein gehört gräflichen "
所以我尝试通过一行测试脚本,直接粘贴到 perl 脚本中,而不是从文件中读取它。那里的输出非常好。我检查了 utf8 unicode 格式的文件。文件仍然必须导致输出问题(?)。
由于脚本太长,我只删了相关的:
(转到目录,打开文件,将输入引导到函数 &align,对其进行分析,将其添加到数组,打印数组)
#!/usr/bin/perl -w
use strict;
use utf8;
binmode(STDIN,":utf8");
binmode(STDOUT,":utf8");
binmode(STDERR,":utf8");
#opens directory
#opens file from directory
if (-d "$dir/$first"){
opendir (UDIR, "$dir/$first") or die "could not open: $!";
foreach my $t (readdir(UDIR)){
next if $first eq ".";
next if $first eq "..";
open(GT,"$dir/$first/$t") or die "Could not open GT, $!";
my $gt= <GT>;
chomp $gt;
#directly pasted lines in perl - creates correct output
&align("det man die Profeſſores der Philoſophie re- ");
#lines from file - output not correct
#&align($gt);
close GT;
next;
}closedir UDIR;
}
有什么想法吗?
你告诉 Perl 你的源代码是 UTF-8,STDIN、STDOUT 和 STDERR 是 UTF-8,但你没有说你正在阅读的文件包含 UTF-8。
open(GT,"<:utf8", "$dir/$first/$t") or die "Could not open GT, $!";
否则,Perl 会假定该文件是用 ISO-8859-1 编码的,因为如果您不指定其他字符集,那将是 Perl 的默认字符集。它有助于将这些 ISO-8859-1 字符转码为 UTF-8 以供输出,因为您已经告诉它 STDOUT 使用 UTF-8。由于该文件实际上是 UTF-8,而不是 ISO-8859-1,因此您得到的输出不正确。
所以我有 utf8 文本文件,我想读入这些文件,将这些行放入一个数组中,然后打印出来。但是输出却没有正确打印符号,例如输出行如下所示:
"arnſtein gehört gräflichen "
所以我尝试通过一行测试脚本,直接粘贴到 perl 脚本中,而不是从文件中读取它。那里的输出非常好。我检查了 utf8 unicode 格式的文件。文件仍然必须导致输出问题(?)。
由于脚本太长,我只删了相关的: (转到目录,打开文件,将输入引导到函数 &align,对其进行分析,将其添加到数组,打印数组)
#!/usr/bin/perl -w
use strict;
use utf8;
binmode(STDIN,":utf8");
binmode(STDOUT,":utf8");
binmode(STDERR,":utf8");
#opens directory
#opens file from directory
if (-d "$dir/$first"){
opendir (UDIR, "$dir/$first") or die "could not open: $!";
foreach my $t (readdir(UDIR)){
next if $first eq ".";
next if $first eq "..";
open(GT,"$dir/$first/$t") or die "Could not open GT, $!";
my $gt= <GT>;
chomp $gt;
#directly pasted lines in perl - creates correct output
&align("det man die Profeſſores der Philoſophie re- ");
#lines from file - output not correct
#&align($gt);
close GT;
next;
}closedir UDIR;
}
有什么想法吗?
你告诉 Perl 你的源代码是 UTF-8,STDIN、STDOUT 和 STDERR 是 UTF-8,但你没有说你正在阅读的文件包含 UTF-8。
open(GT,"<:utf8", "$dir/$first/$t") or die "Could not open GT, $!";
否则,Perl 会假定该文件是用 ISO-8859-1 编码的,因为如果您不指定其他字符集,那将是 Perl 的默认字符集。它有助于将这些 ISO-8859-1 字符转码为 UTF-8 以供输出,因为您已经告诉它 STDOUT 使用 UTF-8。由于该文件实际上是 UTF-8,而不是 ISO-8859-1,因此您得到的输出不正确。