Python_报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence

Python 报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence

    在运行序列化(pickle)相关功能时报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence

上代码:

>>> fp = open("a.txt","r+")
>>> import pickle
>>> pickle.load(fp)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence

 

 原因分析:序列化操作时,文件模式不正确,改为“rb+”,即可

解决:

改成如下方法即可

>>> fp = open("a.txt","rb+")#文件模式为字节处理
>>> import pickle
>>> pickle.load(fp)
['apple', 'mango', 'carrot']

 

posted @ 2018-10-22 21:41  翻滚的小强  阅读(23381)  评论(0编辑  收藏  举报