Python(nltk) - UnicodeDecodeError:' ascii'编解码器不能解码字节

时间:2014-08-25 20:18:52

标签: python error-handling compiler-errors nltk

我是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.")))

4 个答案:

答案 0 :(得分:5)

尝试这个......使用Python 2.7.x的NLTK 3.0.1

import io
f = io.open(txtFile, 'rU', encoding='utf-8')

答案 1 :(得分:4)

我遇到了同样的问题。我在Windows 7中使用Python 3.4。

我安装了&#34; nltk-3.0.0.win32.exe&#34; (来自here)。但是当我安装&#34; nltk-3.0a4.win32.exe&#34; (来自here),我解决了nltk.pos_tag的问题。检查一下。

编辑:如果第二个链接不起作用,您可以查看here

答案 2 :(得分:-2)

重复:NLTK 3 POS_TAG throws UnicodeDecodeError

长话短说:NLTK与Python 3不兼容。你必须使用NLTK 3,这听起来有点实验性。

答案 3 :(得分:-2)

尝试使用模块&#34; textclean&#34;

>>> 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))