错误 UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 0 中的字节 0xff:起始字节无效 [英] error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
问题描述
https://github.com/affinelayer/pix2pix-tensorflow/tree/大师/工具
在上述站点上编译process.py"时发生错误.
An error occurred when compiling "process.py" on the above site.
python tools/process.py --input_dir data -- operation resize --outp
ut_dir data2/resize
data/0.jpg -> data2/resize/0.png
回溯(最近一次调用最后一次):
Traceback (most recent call last):
File "tools/process.py", line 235, in <module>
main()
File "tools/process.py", line 167, in main
src = load(src_path)
File "tools/process.py", line 113, in load
contents = open(path).read()
File"/home/user/anaconda3/envs/tensorflow_2/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
错误的原因是什么?Python 的版本是 3.5.2.
What is the cause of the error? Python's version is 3.5.2.
推荐答案
Python 尝试将字节数组(它假定为 utf-8 编码字符串的 bytes
)转换为unicode 字符串 (str
).这个过程当然是按照utf-8规则进行解码.当它尝试这样做时,它遇到了 utf-8 编码字符串中不允许的字节序列(即位置 0 处的这个 0xff).
Python tries to convert a byte-array (a bytes
which it assumes to be a utf-8-encoded string) to a unicode string (str
). This process of course is a decoding according to utf-8 rules. When it tries this, it encounters a byte sequence which is not allowed in utf-8-encoded strings (namely this 0xff at position 0).
由于您没有提供任何我们可以查看的代码,我们只能猜测其余部分.
Since you did not provide any code we could look at, we only could guess on the rest.
从堆栈跟踪我们可以假设触发操作是从文件中读取(contents = open(path).read()
).我建议以这样的方式重新编码:
From the stack trace we can assume that the triggering action was the reading from a file (contents = open(path).read()
). I propose to recode this in a fashion like this:
with open(path, 'rb') as f:
contents = f.read()
open()
中模式说明符中的 b
声明文件应被视为二进制文件,因此 contents
将保持为字节
.不会以这种方式进行解码尝试.
That b
in the mode specifier in the open()
states that the file shall be treated as binary, so contents
will remain a bytes
. No decoding attempt will happen this way.
这篇关于错误 UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 0 中的字节 0xff:起始字节无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!