两台不同机器上的相同python源代码会产生不同的行为

2024-03-29 12:47:04 发布

您现在位置:Python中文网/ 问答频道 /正文

两台机器,都运行ubuntu14.04.1。相同的源代码运行在相同的数据上。一个工作正常,一个抛出codec decode 0xe2错误。为什么会这样?(更重要的是,我如何修复它?)在

违规代码似乎是:

def tokenize(self):
    """Tokenizes text using NLTK's tokenizer, starting with sentence tokenizing"""
    tokenized=''
    for sentence in sent_tokenize(self):
        tokenized += ' '.join(word_tokenize(sentence)) + '\n'

    return Text(tokenized)

好吧。。。我进入交互模式,从nltk.tokenize在两台机器上。工作人员对以下内容感到满意:

^{pr2}$

出现问题的计算机上的UnicodeDecodeError提供以下回溯:

^{3}$

逐行分解输入文件(通过split('\n')),并通过sent_tokenize运行每个文件,会导致出现问题的行:

If you have purchased these Services directly from Cisco Systems, Inc. (“Cisco”), this document is incorporated into your Master Services Agreement or equivalent services agreement (“MSA”) executed between you and Cisco.

实际上是:

>>> bar[5]
'If you have purchased these Services directly from Cisco Systems, Inc. (\xe2\x80\x9cCisco\xe2\x80\x9d), this document is incorporated into your Master Services Agreement or equivalent services agreement (\xe2\x80\x9cMSA\xe2\x80\x9d) executed between you and Cisco.'

更新:两台机器都显示UnicodeDecodeError:

unicode(bar[5])

但只有一台机器显示以下错误:

sent_tokenize(bar[5])

Tags: 文件self机器youif错误barcisco
1条回答
网友
1楼 · 发布于 2024-03-29 12:47:04

不同的NLTK版本!在

没有barf的版本使用nltk2.0.4;引发异常的版本是3.0.0。在

nltk2.0.4对

sent_tokenize('(\xe2\x80\x9cCisco\xe2\x80\x9d)')

nltk3.0.0需要unicode(正如@tdelaney在上面的评论中指出的)。因此,要获得结果,您需要:

^{pr2}$

相关问题 更多 >