簡體   English   中英

Python(nltk)-UnicodeDecodeError:“ ascii”編解碼器無法解碼字節

[英]Python (nltk) - UnicodeDecodeError: 'ascii' codec can't decode byte

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

試試這個... NLTK 3.0.1和Python 2.7.x

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

我也有同樣的問題 我在Windows 7中使用Python 3.4。

我已經安裝了“ nltk-3.0.0.win32.exe”(從這里開始 )。 但是,當我安裝“ nltk-3.0a4.win32.exe”(從此處開始 )時,我的nltk.pos_tag問題已解決。 檢查。

編輯:如果第二個鏈接不起作用,則可以在此處查看

復制: NLTK 3 POS_TAG引發UnicodeDecodeError

長話短說:NLTK與Python 3不兼容。您現在必須使用NLTK 3,這聽起來有點試驗性。

嘗試使用模塊“ 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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM