在Python中使用nltk时出现非ASCII字符错误

时间:2016-03-05 05:41:52

标签: python ascii nltk

我尝试使用以下链接中提供的解决方案代码:Unicode Tagging in Python NLTK

omerbp给出的解决方案中:

from nltk.corpus import indian
from nltk.tag import tnt

train_data = indian.tagged_sents('hindi.pos')
tnt_pos_tagger = tnt.TnT()
tnt_pos_tagger.train(train_data) #Training the tnt Part of speech tagger with hindi data

print tnt_pos_tagger.tag(nltk.word_tokenize(word_to_be_tagged))

我收到以下错误:

  

'语法错误:非ASCII字符' \ xe0'在第1行的文件q12.py中,但没有声明编码;有关详细信息,请参阅http://www.python.org/peps/pep-0263.html'在第1行。

1 个答案:

答案 0 :(得分:1)

在文件顶部添加以下两行:

#!/usr/bin/python
# -*- coding: utf-8 -*-

他们将指示解释器将每个字符编码为UTF-8而不是ASCII。