python教程—Python NLTK: SyntaxError:非ascii字符“xc3”文件形式(Senitment Analysis -NLP)-Python实用宝典

python教程—Python NLTK: SyntaxError:非ascii字符“xc3”文件形式(Senitment Analysis -NLP)

我正在摆弄NLTK做一个关于情绪分析的作业。我用的是python2.7。NLTK 3.0和NUMPY 1.9.1版本。

我正在摆弄NLTK做一个关于情绪分析的作业。我用的是python2.7。NLTK 3.0和NUMPY 1.9.1版本。

这是代码:

    __author__ = 'karan' import nltk import re import sys def main(): print("Start"); # getting the stop words stopWords = open("english.txt","r"); stop_word = stopWords.read().split(); AllStopWrd = [] for wd in stop_word: AllStopWrd.append(wd); print("stop words-> ",AllStopWrd); # sample and also cleaning it tweet1= 'Love, my new toyí ½í¸í ½í¸#iPhone6. Its good http://t.co/sHY1cab7sx' print("old tweet-> ",tweet1) tweet1 = tweet1.lower() tweet1 = ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z t])|(w+://S+)"," ",tweet1).split()) print(tweet1); tw = tweet1.split() print(tw) #tokenize sentences = nltk.word_tokenize(tweet1) print("tokenized ->", sentences) #remove stop words Otweet =[] for w in tw: if w not in AllStopWrd: Otweet.append(w); print("sans stop word-> ",Otweet) # get taggers for neg/pos/inc/dec/inv words taggers ={} negWords = open("neg.txt","r"); neg_word = negWords.read().split(); print("ned words-> ",neg_word) posWords = open("pos.txt","r"); pos_word = posWords.read().split(); print("pos words-> ",pos_word) incrWords = open("incr.txt","r"); inc_word = incrWords.read().split(); print("incr words-> ",inc_word) decrWords = open("decr.txt","r"); dec_word = decrWords.read().split(); print("dec wrds-> ",dec_word) invWords = open("inverse.txt","r"); inv_word = invWords.read().split(); print("inverse words-> ",inv_word) for nw in neg_word: taggers.update({nw:'negative'}); for pw in pos_word: taggers.update({pw:'positive'}); for iw in inc_word: taggers.update({iw:'inc'}); for dw in dec_word: taggers.update({dw:'dec'}); for ivw in inv_word: taggers.update({ivw:'inv'}); print("tagger-> ",taggers) print(taggers.get('little')) # get parts of speech posTagger = [nltk.pos_tag(tw)] print("posTagger-> ",posTagger) main();

这是我运行我的代码时得到的错误:

    SyntaxError: Non-ASCII character 'xc3' in file C:/Users/karan/PycharmProjects/mainProject/sentiment.py on line 19, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

我如何修正这个错误?

我还尝试了使用Python 3.4.2和nltk 3.0以及NumPy 1.9.1编写代码,但是我得到了一个错误:

    Traceback (most recent call last): File "C:/Users/karan/PycharmProjects/mainProject/sentiment.py", line 80, in <module> main(); File "C:/Users/karan/PycharmProjects/mainProject/sentiment.py", line 72, in main posTagger = [nltk.pos_tag(tw)] File "C:Python34libsite-packagesnltktag__init__.py", line 100, in pos_tag tagger = load(_POS_TAGGER) File "C:Python34libsite-packagesnltkdata.py", line 779, in load resource_val = pickle.load(opened_resource) UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0: ordinal not in range(128)

回答

将以下代码添加到文件的顶部# coding=utf-8

如果你去到错误的链接,你可以看到原因:

< <强>定义编码/强>

Python将默认为ASCII作为标准编码,如果没有其他编码
给出了编码提示。
要定义源代码编码,必须有一个神奇的注释
以第一或第二的形式放入源文件中
文件中的行,如:
#编码= < / em >

​Python实用宝典 (pythondict.com)
不只是一个宝典
欢迎关注公众号:Python实用宝典

本文由 Python实用宝典 作者:Python实用宝典 发表,其版权均为 Python实用宝典 所有,文章内容系作者个人观点,不代表 Python实用宝典 对观点赞同或支持。如需转载,请注明文章来源。
0

发表评论