admin管理员组文章数量:1610854
一、问题
博主在读取pkl文件时出现以下错误:
ascii’ codec can’t decode byte 0xe4 in position 0: ordinal not in range(128)
unicode 最大长度为128,利用Python在写入pkl文件时,unicode 则会被作为“中间编码”。因此将读取进来的ascii编码字符串如果超过128则会报错
二、代码
在pickle.load()第二个参数加上encoding=‘bytes’,读取的pkl文件中含有的字符串需要加上.encode('utf-8')
import sys
if sys.version_info[0] == 2:
import cPickle as pickle
else:
import pickle
def load_pickle(path, verbose=True):
"""Check and load pickle object.
According to this post: https://stackoverflow/a/41733927, cPickle and
disabling garbage collector helps with loading speed."""
assert osp.exists(path), "File not exists: {}".format(path)
# gc.disable()
with open(path, 'rb') as f:
# check the python version
if sys.version_info.major == 3:
ret = pickle.load(f, encoding='bytes')
elif sys.version_info.major == 2:
ret = pickle.load(f)
# gc.enable()
if verbose:
print('Loaded pickle file {}'.format(path))
return ret
load_path = '你的pkl文件路径'
pkl = load_pickle(load_path)
key_byte = '你的key'.encode('utf-8')
item = pkl[key_byte]
本文标签: 解决方法CodecDecodeASCIIPython
版权声明:本文标题:Python 中pickle出现 ascii’ codec can’t decode byte 0xe4 in position 0: ordinal not in range(128) 解决方法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728606618a1165535.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论