Python泡菜错误:UnicodeDecodeError [英] Python pickle error: UnicodeDecodeError

查看:90
本文介绍了Python泡菜错误:UnicodeDecodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Textblob进行一些文本分类.我首先训练模型并使用pickle对其进行序列化,如下所示.

I'm trying to do some text classification using Textblob. I'm first training the model and serializing it using pickle as shown below.

import pickle
from textblob.classifiers import NaiveBayesClassifier

with open('sample.csv', 'r') as fp:
     cl = NaiveBayesClassifier(fp, format="csv")

f = open('sample_classifier.pickle', 'wb')
pickle.dump(cl, f)
f.close()

当我尝试运行此文件时:

And when I try to run this file:

import pickle
f = open('sample_classifier.pickle', encoding="utf8")
cl = pickle.load(f)    
f.close()

我收到此错误:

UnicodeDecodeError:'utf-8'编解码器无法解码位置的字节0x80 0:无效的起始字节

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

以下是我的sample.csv的内容:

Following are the content of my sample.csv:

我的SQL根本无法正常工作.这是一个错误的选择,SQL

My SQL is not working correctly at all. This was a wrong choice, SQL

我有问题.请立即回复,支持

I've issues. Please respond immediately, Support

我在哪里错了?请帮忙.

Where am I going wrong here? Please help.

推荐答案

通过选择在模式wbopen该文件,您选择以原始二进制文件编写.没有应用字符编码.

By choosing to open the file in mode wb, you are choosing to write in raw binary. There is no character encoding being applied.

因此,要读取此文件,只需在rb模式下单击open.

Thus to read this file, you should simply open in mode rb.

这篇关于Python泡菜错误:UnicodeDecodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆