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

Posted 翻滚的小强

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python_报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal 相关的知识,希望对你有一定的参考价值。

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 cant decode byte 0x80 in position 0: illegal multibyte sequence

 

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

解决:

改成如下方法即可

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

 

以上是关于Python_报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal 的主要内容,如果未能解决你的问题,请参考以下文章

7_python下Crypto模块报错

Python报错:TypeError: __init__() got an unexpected keyword argument 'io_loop' 原因及解决方案

python_异常报错

python_执行报错_No module named 'pandas'

Python 同级目录import报错

Python_报错:SyntaxError: EOL while scanning string literal