UnicodeDecodeError: 'xxx' codec can't decode byte 0xxx in position 0: xxx
程序员文章站
2022-05-27 10:39:16
...
在环境python3.6中,使用Python2.7的一些代码时发生了错误(不兼容)
代码:
引起报错的写法:
1,f = io.open('/user/weights.save', 'rb')
...
cPickle.load(f)
2,with open('/user/weights.save', 'rb') as f:
...
cPickle.load(f)
报错:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf2 in position 0: ordinal not in range(128)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf2 in position 0: invalid continuation byte
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
原因:可能是读取weights.save的解码格式不对,不能使用'ascii', 'utf-8', 'gbk'。错误的地方在cPickle.load(f),不在 open那一句
解决:使用 'latin1' 解码格式
import pickle as cPickle
model_weighs_file = '/user/weights.save'
with open(model_weighs_file, 'rb') as f:
loaded_objects = []
for i in range(5):
loaded_objects.append(cPickle.load(f, encoding='latin1'))
推荐阅读
-
Anaconda中启动Python时的错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 553
-
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xcb in position 260: ordinal not in range(128)
-
命令行调用python出现编码错误:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x9a in position 140 完美解决!
-
Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
-
Python “'ascii' codec can't decode byte 0xb0i in position 1” 的解决方法
-
真正解决Windows下UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xff in position 0错误的方法
-
当在命令行中执行virtualenv venv时报此错误:'utf-8' codec can't decode byte 0xd5 in position 38: invalid continuation by
-
UnicodeDecodeError: 'utf-8' codec can't decode byte in position : invalid continuation byte
-
【debug】UnicodeDecodeError: codec can't decode byte 0xbd in position 4: invalid start byte
-
python3 报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6