我一直在關注這個網站,https://radimrehurek.com/data_science_python/,以在推文列表中應用單詞包。Python袋單詞NameError:名稱'unicode'沒有定義
import csv
from textblob import TextBlob
import pandas
messages = pandas.read_csv('C:/Users/Suki/Project/Project12/newData1.csv', sep='\t', quoting=csv.QUOTE_NONE,
names=["label", "message"])
def split_into_tokens(message):
message = unicode(message, encoding="utf8") # convert bytes into proper unicode
return TextBlob(message).words
messages.message.head().apply(split_into_tokens)
print (messages)
但是我不斷收到此錯誤。我已經檢查過,並且遵循網站上的代碼,但錯誤不斷出現。
錯誤
Traceback (most recent call last):
File "C:/Users/Suki/Project/Project12/projectBagofWords.py", line 34, in <module>
messages.message.head().apply(split_into_tokens)
File "C:\Program Files\Python36\lib\site-packages\pandas\core\series.py", line 2510, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/src\inference.pyx", line 1521, in pandas._libs.lib.map_infer
File "C:/Users/Suki/Project/Project12/projectBagofWords.py", line 31, in split_into_tokens
message = unicode(message, encoding="utf8") # convert bytes into proper unicode
NameError: name 'unicode' is not defined
可我怎麼能糾正這一點有人獻計獻策?
謝謝
感謝您的答覆。我並不知道它是Python 2,因爲我最近纔開始使用Python和3.在糾正錯誤之後,我最初遇到了代碼中的另一個錯誤。試圖在Python 3中使用Python 2時常見嗎? – Estra