導入文本,其中包含ÅÄÖ,我最終想要實現的是將文件中的所有小寫字符轉換爲大寫,並將它們寫入終端。Perl:從文件
use utf8;
binmode STDOUT, ":utf8";
$text = "ABCÅÄÖ\n";
$text =~ tr/A-Ö/a-ö/;
print $text;
輸出:
abcåäö
正如預期的那樣。
但是,當我嘗試從文件中導入相同的文本,它變得瘋狂。
use utf8;
binmode STDOUT, ":utf8";
open FILE, $filename or die "An error occurred while reading the file: $!";
$text = join '', <FILE>;
close FILE or die "An error occurred while closing the file: $!";
$text =~ tr/A-Ö/a-ö/;
print $text;
輸出
ABCÃÃÃ
我假設導入的文本不正確編碼。任何人都知道如何在導入時對文本進行編碼?
在此先感謝。
傑克
這個工作更好,與tr和所有:) – 2014-11-24 15:52:21
那麼,依靠'$ text =〜tr/A-Ö/ a-ö/'是不安全的。使用'lc($ text)'或'$ text =〜s /([A-ZÅÄÖ])/ \ L $ 1/g'。我做了一些其他的改進(詞法代替全局變量,3-arg打開,包括錯誤消息中的文件名,...) – ikegami 2014-11-24 16:39:03