python 3以上版本使用pickle.load读取文件报UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6

原本代码是这样的

    fo = open(file, 'rb')
    dict = pickle.load(fo)

修改之后只需要在打开的时候指定编码

 fo = open(file, 'rb')

   dict = pickle.load(fo,encoding='iso-8859-1')


python2的时候是

import sys  
reload(sys)  
sys.setdefaultencoding('utf8')
在python3.X中已经不再使用

猜你喜欢

转载自blog.csdn.net/qq_33144323/article/details/80042273