這涉及到以下幾個問題 -的UnicodeDecodeError: 'ASCII' 編解碼器不能解碼字節 - Python的
- Python unicode equal comparison failed
- Find word infront and behind of a Python list
- Searching for Unicode characters in Python
- NLTK Context Free Grammar Genaration
我有python應用程序執行以下任務 -
# -*- coding: utf-8 -*-
1.閱讀Unicode文本文件(非英語) -
def readfile(file, access, encoding):
with codecs.open(file, access, encoding) as f:
return f.read()
text = readfile('teststory.txt','r','utf-8-sig')
這給予回報的文本文件作爲字符串。
2.將文本分割成句子。
3.經過每一句話,並確定動詞,名詞等
參考 - Searching for Unicode characters in Python和Find word infront and behind of a Python list
4.添加他們到不同的變量如下
名詞=「CAR」| 「BUS」|
verbs =「DRIVES」| 「命中」
5.現在我想將它們傳遞到NLTK背景如下自由語法 -
grammar = nltk.parse_cfg('''
S -> NP VP
NP -> N
VP -> V | NP V
N -> '''+nouns+'''
V -> '''+verbs+'''
''')
它給了我下面的錯誤 -
line 40, in V -> '''+verbs+''' UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 114: ordinal not in range(128)
哪能克服這個問題並將變量傳遞給NLTK CFG?
完整代碼 - https://dl.dropboxusercontent.com/u/4959382/new.zip
你可以顯示錯誤的* full * traceback嗎? – Bakuriu
我正在使用Pycharm。我如何打印完整的追溯? print_stack()不起作用。無論如何,可以找出與給定的例外問題? – ChamingaD
'輸入日誌;嘗試:你的代碼;除了:logging.exception(「ouch」)'#爲了清楚起見,使用換行符和縮進代替';' –