2015-12-30 57 views
0

我剛開始用NLTK,當我嘗試安裝裝配與NLTK在VS2012 IDE 蟒蛇第一我運行:Python和VS2012

import nltk 
nltk.download() 

它運行correctly.Then我嘗試:

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

回答

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" 

另一種方式是通過與r前綴它來告訴Python,字符串是原始字符串

NLTK_DADA = r"E:\NLtk\nltk_data" 

string literals的蟒蛇。