我有一个生成UTF-8文件的应用程序,但有些内容编码错误。一些字符编码为iso-8859-1 aka iso-latin-1或cp1252 aka Windows-1252。有没有办法恢复原始文本?
答案 0 :(得分:11)
是!
显然,修复程序创建文件会更好,但这并不总是可行的。以下是两种解决方案。
Encoding::FixLatin提供了一个名为fix_latin
的函数,它解码由UTF-8,iso-8859-1,cp1252和US-ASCII组合而成的文本。
$ perl -e'
use Encoding::FixLatin qw( fix_latin );
$bytes = "\xD0 \x92 \xD0\x92\n";
$text = fix_latin($bytes);
printf("U+%v04X\n", $text);
'
U+00D0.0020.2019.0020.0412.000A
启用了启发式算法,但它们相当可靠。只有以下情况才会失败:
使用iso-8859-1或cp1252进行编码,然后用[€,ƒ“...†” <NBSP>
¡¢£¤¥|§¨©ª«¬<SHY>
®°±²³'μ¶·¸¹º»¼½¾¿ ]使用iso-8859-1或cp1252进行编码。
使用iso-8859-1或cp1252编码的[àáâããååæçèéêëìïï]中的一个,然后是两个[€,ƒ“...†‡‡‰<ŒŽ ''“”•--~™S>œžŸ<NBSP>
¡¢£¤¥|§¨©ª«¬<SHY>
®°±²³'μ¶·¸¹º»¼½¾¿] <使用iso-8859-1或cp1252进行编码。
使用iso-8859-1或cp1252编码的[ðñòóôõö÷]中的一个,然后是两个[€,ƒ“...†‡‰Š< OEZ ''“”•--~™S>œžŸ<NBSP>
¡¢£¤¥|§¨©ª«¬<SHY>
®°±²³'μ¶·¸¹º»¼½¾¿]
使用iso-8859-1或cp1252编码。
使用核心模块Encode可以产生相同的结果,但我想这比安装了Encoding :: FixLatin :: XS的Encoding :: FixLatin慢一点。
$ perl -e'
use Encode qw( decode_utf8 encode_utf8 decode );
$bytes = "\xD0 \x92 \xD0\x92\n";
$text = decode_utf8($bytes, sub { encode_utf8(decode("cp1252", chr($_[0]))) });
printf("U+%v04X\n", $text);
'
U+00D0.0020.2019.0020.0412.000A
fix_latin
适用于角色等级。如果已知每条线都使用UTF-8,iso-8859-1,cp1252或US-ASCII之一进行完全编码,则可以通过检查线路是否为有效UTF-8来使该过程更加可靠。
$ perl -e'
use Encode qw( decode );
for $bytes ("\xD0 \x92 \xD0\x92\n", "\xD0\x92\n") {
if (!eval {
$text = decode("UTF-8", $bytes, Encode::FB_CROAK|Encode::LEAVE_SRC);
1 # No exception
}) {
$text = decode("cp1252", $bytes);
}
printf("U+%v04X\n", $text);
}
'
U+00D0.0020.2019.0020.00D0.2019.000A
U+0412.000A
启用了启发式算法,但它们非常可靠。只有在给定行的所有以下内容为真时,它们才会失败:
该行使用iso-8859-1或cp1252编码,
至少有一个[€,ƒ“...†‡‰<ŒŽ''”“• - 〜™š>œžŸ<NBSP>
¢¢¥ §©ª«¬<SHY>
®°±²³'μ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷]
存在于线,
所有实例中的所有实例都始终紧跟着[€,ƒ“...†‡‰Š<<ŒŽ''”“ •--~™S>œžŸ<NBSP>
¡¢£¤¥|§¨©ª«¬<SHY>
®°±²³'μ¶·¸¹º»¼½¾¿]
所有实例中的所有实例总是紧跟两个[€,ƒ“...†‡‰Š<ŒŽ''”“ - -~™S>œžŸ<NBSP>
¡¢£¤¥|§¨©ª«¬<SHY>
®°±²³'μ¶·¸¹º»¼½¾¿]
所有[ðñòóôõö÷]的实例总是紧跟三个[€,ƒ“...†‡‰Œ<<ŒŽ''”“ --~™S>œžŸ<NBSP>
¡¢£¤¥|§¨©ª«¬<SHY>
®°±²³'μ¶·¸¹º»¼½¾¿]
行中没有[øùúûüýþÿ]
,
没有[€,ƒ“...†‡‡<ŒŽ''”“• - 〜™š>œžŸ<NBSP>
¡£¤¥|§除了前面提到的以外,行中都有©ª{¬<SHY>
®°±³³'μ¶·³¹º»¼½¾¿]。
注意:
fix_latin
来转换文件,使用第二种方法编写一个文件是微不足道的。fix_latin
(功能和文件)可以通过安装Encoding::FixLatin::XS来加速。答案 1 :(得分:5)
这是我写Unicode::UTF8的原因之一。使用Unicode :: UTF8,使用Unicode::UTF8::decode_utf8()中的后备选项很简单。
use Unicode::UTF8 qw[decode_utf8];
use Encode qw[decode];
print "UTF-8 mixed with Latin-1 (ISO-8859-1):\n";
for my $octets ("\xD0 \x92 \xD0\x92\n", "\xD0\x92\n") {
no warnings 'utf8';
printf "U+%v04X\n", decode_utf8($octets, sub { $_[0] });
}
print "\nUTF-8 mixed with CP-1252 (Windows-1252):\n";
for my $octets ("\xD0 \x92 \xD0\x92\n", "\xD0\x92\n") {
no warnings 'utf8';
printf "U+%v04X\n", decode_utf8($octets, sub { decode('CP-1252', $_[0]) });
}
输出:
UTF-8 mixed with Latin-1 (ISO-8859-1):
U+00D0.0020.0092.0020.0412.000A
U+0412.000A
UTF-8 mixed with CP-1252 (Windows-1252):
U+00D0.0020.2019.0020.0412.000A
U+0412.000A
Unicode :: UTF8是用C / XS编写的,只在遇到生成错误的UTF-8序列时调用回调/回退。
答案 2 :(得分:-1)
最近我遇到了严重混合的UTF-8,CP1252和UTF-8编码的文件,然后解释为CP1252,然后再次编码为UTF-8,再次解释为CP1252,依此类推。 / p>
我写了下面的代码,对我来说效果很好。它查找典型的UTF-8字节序列,即使某些字节不是UTF-8,也是等效CP1252字节的Unicode表示。
my %cp1252Encoding = (
# replacing the unicode code with the original CP1252 code
# see e.g. http://www.i18nqa.com/debug/table-iso8859-1-vs-windows-1252.html
"\x{20ac}" => "\x80",
"\x{201a}" => "\x82",
"\x{0192}" => "\x83",
"\x{201e}" => "\x84",
"\x{2026}" => "\x85",
"\x{2020}" => "\x86",
"\x{2021}" => "\x87",
"\x{02c6}" => "\x88",
"\x{2030}" => "\x89",
"\x{0160}" => "\x8a",
"\x{2039}" => "\x8b",
"\x{0152}" => "\x8c",
"\x{017d}" => "\x8e",
"\x{2018}" => "\x91",
"\x{2019}" => "\x92",
"\x{201c}" => "\x93",
"\x{201d}" => "\x94",
"\x{2022}" => "\x95",
"\x{2013}" => "\x96",
"\x{2014}" => "\x97",
"\x{02dc}" => "\x98",
"\x{2122}" => "\x99",
"\x{0161}" => "\x9a",
"\x{203a}" => "\x9b",
"\x{0153}" => "\x9c",
"\x{017e}" => "\x9e",
"\x{0178}" => "\x9f",
);
my $re = join "|", keys %cp1252Encoding;
$re = qr/$re/;
my %cp1252Decoding = reverse % cp1252Encoding;
my $cp1252Characters = join "|", keys %cp1252Decoding;
sub decodeUtf8
{
my ($str) = @_;
$str =~ s/$re/ $cp1252Encoding{$&} /eg;
utf8::decode($str);
return $str;
}
sub fixString
{
my ($str) = @_;
my $r = qr/[\x80-\xBF]|$re/;
my $current;
do {
$current = $str;
# If this matches, the string is likely double-encoded UTF-8. Try to decode
$str =~ s/[\xF0-\xF7]$r$r$r|[\xE0-\xEF]$r$r|[\xC0-\xDF]$r/ decodeUtf8($&) /eg;
} while ($str ne $current);
# decodes any possible left-over cp1252 codes to Unicode
$str =~ s/$cp1252Characters/ $cp1252Decoding{$&} /eg;
return $str;
}
这与ikegami的答案有类似的限制,除了相同的限制也适用于UTF-8编码的字符串。