Python(nltk)- UnicodeDecodeError:'ascii'编解码器无法解码字节

9

我是 NLTK 的新手。我遇到了以下这个错误,我已经查找了有关编码/解码和特别是 UnicodeDecodeError 的相关信息,但此错误似乎是针对 NLTK 源代码的。

错误信息如下:

Traceback (most recent call last):
  File "A:\Python\Projects\Test\main.py", line 2, in <module>
    print(pos_tag(word_tokenize("John's big idea isn't all that bad.")))
  File "A:\Python\Python\lib\site-packages\nltk\tag\__init__.py", line 100, in pos_tag
    tagger = load(_POS_TAGGER)
  File "A:\Python\Python\lib\site-packages\nltk\data.py", line 779, in load
    resource_val = pickle.load(opened_resource)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0: ordinal not in range(128)

我该如何解决这个错误?
以下是导致错误的原因:
from nltk import pos_tag, word_tokenize
print(pos_tag(word_tokenize("John's big idea isn't all that bad.")))

是 pos_tag 函数导致了错误。 - user3422952
在您展示的代码中没有任何会产生错误的内容。请打印您传递的字符串的repr - Mark Ransom
返回字符串,但用 ' 包围它。 - user3422952
@MarkRansom 我不知道你的意思,函数 pos_tag 导致了错误。我认为编码错误是在 pickle.load 函数上生成的。我不确定该怎么办。 - user3422952
4个回答

5
尝试这个...使用Python 2.7.x和NLTK 3.0.1。
import io
f = io.open(txtFile, 'rU', encoding='utf-8')

运行得非常好!我使用nltk 3.1和Python 2.7.x。 - Muffintop
太好了!您能否解释一下为什么使用io可以解决这个问题? - minocha

4
我和你遇到了同样的问题。我在Windows 7中使用Python 3.4。我已经安装了“nltk-3.0.0.win32.exe”(从这里)。但是当我安装了“nltk-3.0a4.win32.exe”(从这里)后,我的nltk.pos_tag问题得到了解决。请检查一下。
编辑:如果第二个链接无法使用,您可以看这里

第二个链接似乎已经失效了。你有其他的链接吗? - Arnab Chakraborty

-2
尝试使用模块“textclean”。
>>> pip install textclean

Python 代码

from textclean.textclean import textclean
text = textclean.clean("John's big idea isn't all that bad.")
print pos_tag(word_tokenize(text))

这个模块听起来像是一个非常糟糕的想法。特别是在这里,因为错误发生在尝试解码pickle时——pickle是一种结构化数据格式,如果你盲目地“清理”它,你将无法修复它。 - Eevee

-2

4
我正在使用NLTK 3和Python 3.4,但仍然遇到这个错误。 - Gustavo Puma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接