问题描述
在py2.7当中,用以下语句将变量存储到pickle当中
with open('../dataset/m_7800/rf_preds.pickle', 'wb') as f:pickle.dump(rf_preds, f)
这个时候,用py3.6中的pickle取获取存储好的变量
with open('../dataset/m_7800/rf_preds.pickle', 'rb') as f:rf_preds = pickle.load(f)
会报错
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-21-8326d2e71e16> in <module>()1 with open('../dataset/m_7800/rf_preds.pickle', 'rb') as f:
----> 2 rf_preds = pickle.load(f)UnicodeDecodeError: 'ascii' codec can't decode byte 0xb1 in position 8: ordinal not in range(128)
解决方案
在py3.6的load的过程当中加入encoding=’latin1’即可。
with open('../dataset/m_7800/rf_preds.pickle', 'rb') as f:rf_preds = pickle.load(f, encoding='latin1')
如果仍旧不行,可以在py2.7存储的时候加入protocol=2,再用上面的方式进行load操作,如下
with open('../dataset/m_7800/rf_preds.pickle', 'wb') as f:pickle.dump(rf_preds, f, protocol=2)