当我尝试在VS2012 IDE中使用python安装NLTK时,我刚开始使用NLTK 首先我跑:
import nltk
nltk.download()
它运行正常。然后我尝试:
from nltk.book import *
它给了我:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 1
06, in exec_file
exec_code(code, file, global_variables)
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 8
1, in exec_code
code_obj = compile(code, file, 'exec')
File "C:\Users\Toshiba\Documents\Visual Studio 2012\Projects\Helloworld\Hellow
orld\module2.py", line 2
NLTK_DADA ="E:\NLtk\nltk_data"
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in positio
n 2-3: malformed \N character escape
Press any key to continue . . .
答案 0 :(得分:2)
错误本身就说明了一切:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in positio
n 2-3: malformed \N character escape
您正在使用的路径格式错误,因为python。你需要转义\
字符本身,否则python认为你正在逃避\
之后的字符。
正确的字符串格式:
NLTK_DADA = "E:\\NLtk\\nltk_data"
另一种方法是告诉python该字符串是原始字符串,前缀为r
:
NLTK_DADA = r"E:\NLtk\nltk_data"
请参阅python中的string literals。