欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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'))