Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Perl中读取UTF-8并输出为ISO-8859-1_Perl_File Io_Utf 8_Iso 8859 1 - Fatal编程技术网

在Perl中读取UTF-8并输出为ISO-8859-1

在Perl中读取UTF-8并输出为ISO-8859-1,perl,file-io,utf-8,iso-8859-1,Perl,File Io,Utf 8,Iso 8859 1,我必须用Perl读取一个文本文件,该文件编码为UTF-8,工作正常。我的输出文件OUT_2必须编码为ISO-8859-1(也称为“Latin1”)。我尝试了这段代码(还有更多),但我的输出文件OUT_2总是以UTF-8的形式编写的。有什么想法可以实现吗 use strict; use Encode::Encoder; open IN, "c:/Temp/Input.txt"; # this file is UTF-8 open OUT_1, ">", "c:/Temp/out_1.tx

我必须用Perl读取一个文本文件,该文件编码为UTF-8,工作正常。我的输出文件OUT_2必须编码为ISO-8859-1(也称为“Latin1”)。我尝试了这段代码(还有更多),但我的输出文件OUT_2总是以UTF-8的形式编写的。有什么想法可以实现吗

use strict;
use Encode::Encoder;

open IN, "c:/Temp/Input.txt"; # this file is UTF-8

open OUT_1, ">", "c:/Temp/out_1.txt"; 
# encoding of OUT_1 does not matter because it contains only ASCII
open OUT_2, ">:encoding(latin1)", "c:/Temp/out_2.txt"; 

my $line = 1;
while ( <IN> ) {
    chomp;
    print OUT_1 "Write line $line\n";
    print OUT_2 "$_ and some stuff\n";
    $line++;
}

close IN;
close OUT_1;
close OUT_2;

我认为您正在寻找
Encode::Encode($encodeding_out,Encode::decode($encodeding_in,$data))$encoding\u in
$encoding\u out
分别为UTF-8和Latin1(iso-8859-1)


如果UTF-8中有不可打印的字符,则由于UTF-8中的可能字符集大于拉丁语-1中的可能字符集,因此无法进行无损转换。任何不翻译的字符都将被替换为“?”。

我认为您正在寻找
Encode::Encode($encodeding\u out,Encode::decode($encodeding\u in,$data))$encoding\u in
$encoding\u out
分别为UTF-8和Latin1(iso-8859-1)


如果UTF-8中有不可打印的字符,则由于UTF-8中的可能字符集大于拉丁语-1中的可能字符集,因此无法进行无损转换。任何不翻译的字符都将被替换为“?”。

这似乎工作正常(请参阅Perl函数的说明;无需使用在八位字节级别显式转换Perl字符串)(进一步,可能请参阅和函数的说明):

还有
od-ch

$ od -ch input-file-name
0000000   a   a   a 302 243 302 243 302 243   z   z   z  \n
           6161    c261    c2a3    c2a3    7aa3    7a7a    000a
0000015

$ od -ch output-file-name
0000000   a   a   a 243 243 243   z   z   z  \n
           6161    a361    a3a3    7a7a    0a7a
0000012

(我的文件中包含“aaa(zzz)”)

这似乎工作正常(请参阅Perl函数的说明;不需要使用显式转换八位字节级别的Perl字符串)(进一步,可能请参阅和函数的说明):

还有
od-ch

$ od -ch input-file-name
0000000   a   a   a 302 243 302 243 302 243   z   z   z  \n
           6161    c261    c2a3    c2a3    7aa3    7a7a    000a
0000015

$ od -ch output-file-name
0000000   a   a   a 243 243 243   z   z   z  \n
           6161    a361    a3a3    7a7a    0a7a
0000012

(我的文件中包含“aaa(zzz)”)

您的输入中是否包含非ASCII字符?是的,但都适合ISO-8859-1您的输入中是否包含非ASCII字符?是的,但都适合ISO-8859-1我以前从未使用过perl,所以我想知道您的脚本,我应该把我要转换的文件名放在哪里?@zwlayer:我已经更新了我的答案,希望能让它更清楚。非常感谢。我现在明白了。希望有一天,有人会来为那些想使用perl代码片段而不知道如何阅读perl的人做一个解释:)@zwlayer:我真的希望不会。当人们真正花时间去理解他们正在编写的代码时,你会得到更好的程序:-)当PL不是perl时,我同意你的观点:)我打赌perl不是为读而设计的,而是为写而设计的。我以前从未使用过perl,所以我想知道你的脚本,我应该把我要转换的文件名放在哪里?@zwlayer:我已经更新了我的答案,希望能让它更清楚。非常感谢。我现在明白了。希望有一天,有人会来为那些想使用perl代码片段而不知道如何阅读perl的人做一个解释:)@zwlayer:我真的希望不会。当人们真正花时间去理解他们正在编写的代码时,你会得到更好的程序:-)当PL不是perl时,我同意你的观点:)我打赌perl不是为读而设计的,而是为写而设计的。
$ file input-file-name output-file-name
input-file-name: UTF-8 Unicode text
output-file-name:  ISO-8859 text
$ od -ch input-file-name
0000000   a   a   a 302 243 302 243 302 243   z   z   z  \n
           6161    c261    c2a3    c2a3    7aa3    7a7a    000a
0000015

$ od -ch output-file-name
0000000   a   a   a 243 243 243   z   z   z  \n
           6161    a361    a3a3    7a7a    0a7a
0000012