Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NLTK snowball词干分析器UnicodeDecodeError在终端中,但不是Eclipse PyDev_Python_Python 2.7_Pydev_Nltk_Snowball - Fatal编程技术网

Python NLTK snowball词干分析器UnicodeDecodeError在终端中,但不是Eclipse PyDev

Python NLTK snowball词干分析器UnicodeDecodeError在终端中,但不是Eclipse PyDev,python,python-2.7,pydev,nltk,snowball,Python,Python 2.7,Pydev,Nltk,Snowball,我正在使用snowball词干分析器对文档中的单词进行词干处理,如下面的代码片段所示 stemmer = EnglishStemmer() # Stem, lowercase, substitute all punctuations, remove stopwords. attribute_names = [stemmer.stem(token.lower()) for token in wordpunct_tokenize(re.sub('[%s]' % re.esca

我正在使用snowball词干分析器对文档中的单词进行词干处理,如下面的代码片段所示

    stemmer = EnglishStemmer()
    # Stem, lowercase, substitute all punctuations, remove stopwords.
    attribute_names = [stemmer.stem(token.lower()) for token in wordpunct_tokenize(re.sub('[%s]' % re.escape(string.punctuation), '', doc)) if token.lower() not in stopwords.words('english')]
当我在Eclipse中使用PyDev在文档上运行时,没有收到任何错误。当我在终端(Mac OSX)中运行它时,我收到以下错误。有人能帮忙吗

File "data_processing.py", line 171, in __filter__
attribute_names = [stemmer.stem(token.lower()) for token in   wordpunct_tokenize(re.sub('[%s]' % re.escape(string.punctuation), '', doc)) if token.lower()     not in stopwords.words('english')]

File "7.3/lib/python2.7/site-packages/nltk-2.0.4-py2.7.egg/nltk/stem/snowball.py", line   694, in stem
word = (word.replace(u"\u2019", u"\x27")

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 7: ordinal not in range(128)

这在PyDev中起作用,因为它将Python自身配置为在控制台的编码中工作(通常是UTF-8)

如果转到运行配置(run>runconfigurations),然后在“common”选项卡上说希望编码为ascii,则可以在PyDev中重现相同的错误

之所以会发生这种情况,是因为您的单词是一个字符串,并且要用unicode字符替换

我希望下面的代码能为您带来一些启示:

这一切都将ascii作为默认编码:

>>> 'íã'.replace(u"\u2019", u"\x27")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position 0: ordinal not in range(128)
因此,可以在替换之前将字符串设置为unicode

>>> 'íã'.decode('cp850').replace(u"\u2019", u"\x27")
u'\xed\xe3'
或者您可以对替换字符进行编码

>>> 'íã'.replace(u"\u2019".encode('utf-8'), u"\x27".encode('utf-8'))
'\xa1\xc6'

但是请注意,您必须知道您在任何地方使用的实际编码是什么(因此,尽管我在示例中使用的是cp850或utf-8,但它可能与您必须使用的编码不同)

正如Fabio所述,这是因为Pydev更改了Python的默认编码。您知道,有三种可能的解决方案:

在Pydev之外测试代码

Pydev将对您隐藏编码问题,直到您在Eclipse之外运行代码。因此,不要使用Eclipse的“run”按钮,而是从shell测试代码

不过,我不推荐这样做:这意味着您的开发环境将与您的运行环境不同,这只会导致出错

更改Python的默认编码

您可以更改Python的环境以适应Pydev。这一点在本文中进行了讨论

会告诉你怎么做,也会告诉你为什么不应该

长话短说,不要

阻止Pydev更改Python的默认编码

如果您使用的是Python2,Python的默认编码应该是ascii。因此,与其通过黑客攻击使您的环境成为Pydev的首选,不如强迫Pydev“表现”。讨论了如何做到这一点

>>> 'íã'.replace(u"\u2019".encode('utf-8'), u"\x27".encode('utf-8'))
'\xa1\xc6'