Python 中pickle出现 ascii’ codec can’t decode byte 0xe4 in position 0: ordinal not in range(128) 解决方法

阅读: 评论:0

Python 中pickle出现 ascii’ codec can’t decode byte 0xe4 in position 0: ordinal not in range(128) 解决方法

Python 中pickle出现 ascii’ codec can’t decode byte 0xe4 in position 0: ordinal not in range(128) 解决方法

一、问题

博主在读取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 pickledef load_pickle(path, verbose=True):"""Check and load pickle object.According to this post: , cPickle anddisabling garbage collector helps with loading speed."""ists(path), "File not exists: {}".format(path)# gc.disable()with open(path, 'rb') as f:# check the python versionif 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 retload_path = '你的pkl文件路径'
pkl = load_pickle(load_path)key_byte = '你的key'.encode('utf-8')
item = pkl[key_byte]

 

本文发布于:2024-02-01 01:28:40,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170672212332849.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:解决方法   codec   decode   ascii   Python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23