Perl 6 错误消息:块 <unit> 中格式错误的 UTF-8 [英] Perl 6 error message: Malformed UTF-8 in block <unit>
问题描述
我正在尝试读取下载的 html 文件
I'm trying to read a downloaded html-file
my $file = "sn.html";
my $in_fh = open $file, :r;
my $text = $in_fh.slurp;
然后我收到以下错误消息:
and I get the following error message:
Malformed UTF-8
in block <unit> at prog.p6 line 10
如何避免这种情况并访问文件内容?
How to avoid this and get access to the file's contents?
推荐答案
如果在打开文件时没有指定编码,它将采用 utf8
.显然,您要打开的文件包含无法解释为 UTF-8 的字节.因此出现错误消息.
If you do not specify an encoding when opening a file, it will assume utf8
. Apparently, the file that you wish to open, contains bytes that cannot be interpreted as UTF-8. Hence the error message.
根据您想对文件内容做什么,您可以设置 :bin
命名参数,以二进制模式打开文件.或者您可以使用特殊的 utf8-c8
编码,它将假定为 UTF-8,直到遇到无法编码的字节:在这种情况下,它将生成临时代码点.
Depending on what you want to do with the file contents, you could either set the :bin
named parameter, to have the file opened in binary mode. Or you could use the special utf8-c8
encoding, which will assume UTF-8 until it encounters bytes it cannot encode: in that case it will generate temporary code points.
参见 https://docs.raku.org/language/unicode#UTF8-C8 了解更多信息.
See https://docs.raku.org/language/unicode#UTF8-C8 for more information.
这篇关于Perl 6 错误消息:块 <unit> 中格式错误的 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!