本文介绍了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()

我收到此错误:

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

Following are the content of my sample.csv:

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:43