一、問題
博主在讀取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.com/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]