从Perl文件中读取西里尔字母 [英] Reading Cyrillic characters from file in perl
问题描述
我无法从perl的文件中读取西里尔字母.
I'm having trouble reading Cyrillic characters from a file in perl.
该文本文件用记事本编写,并且包含абвгдежзийклмнопрстуфхцчшщъьюя".这是我的代码:
The text file is written in Notepad and contains "абвгдежзийклмнопрстуфхцчшщъьюя". Here's my code:
#!/usr/bin/perl
use warnings;
use strict;
open FILE, "text.txt" or die $!;
while (<FILE>) {
print $_;
}
如果我使用ANSI编码保存文本文件,则会得到:
If I save the text file using the ANSI encoding, I get:
рстуфхцчшщъыьэюяЁёЄєЇїЎў°∙·№■
如果我使用UTF-8编码将其保存,并且使用了Encode包中的函数解码('UTF-8',$ _),则会得到:
If I save it using the UTF-8 encoding, and I use the function decode('UTF-8', $_) from the package Encode, I get:
Wide character in print at test.pl line 11, <TEXT> line 1.
和一堆不可读的字符.
我在Windows 7x64中使用命令提示符
I'm using the command prompt in windows 7x64
推荐答案
您正在解码输入,但是忘记了"对输出进行编码.
You're decoding your inputs, but "forgot" to encode your outputs.
您的文件可能使用 cp1251 进行编码.
Your file is probably encoded using cp1251.
您的终端期望 cp866 .
使用
use open ':std', ':encoding(cp866)';
use open IO => ':encoding(cp1251)';
open(my $FILE, '<', 'text.txt')
or die $!;
或
use open ':std', ':encoding(cp866)';
open(my $FILE, '<:encoding(cp1251)', 'text.txt')
or die $!;
如果另存为UTF-8,请使用:encoding(UTF-8)
代替:encoding(cp1251)
.
Use :encoding(UTF-8)
instead of :encoding(cp1251)
if you saved as UTF-8.
这篇关于从Perl文件中读取西里尔字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!