【问题标题】:Python : AttributeError: 'NoneType' object has no attribute 'start'Python:AttributeError:'NoneType'对象没有属性'start'
【发布时间】:2018-11-27 15:11:26
【问题描述】:

以下代码属于 NLTK 正则表达式:

import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from nltk.tokenize import sent_tokenize

scene = "Hello how! how are you? what is your problem. Can I solve with 00code for you/ by the way bye. Take care"

match_index = print(re.search("you",scene))
print(match_index.start(),match_index.end())

我得到的错误是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-54-5e13e5437c3e> in <module>()
----> 1 print(match_index.start(),match_index.end())

AttributeError: 'NoneType' object has no attribute 'start'

我已经包含了它的库,但它仍然显示错误。什么 我有什么方法可以处理这个错误?

【问题讨论】:

    标签: python python-3.x jupyter-notebook nltk nltk-book


    【解决方案1】:
    match_index = print(re.search("you",scene))
    

    print 返回 None,所以在这一行之后,match_index 是 None。

    尝试在不同的行上分配和打印。

    match_index = re.search("you",scene)
    print(match_index)
    

    结果:

    <_sre.SRE_Match object; span=(19, 22), match='you'>
    19 22
    

    【讨论】:

    • 先生,我执行了这样的代码:match_index = print(re.search("you",scene))print(match_index)print(match_index.start(),match_index.end())
    • 正如我所说,match_index = print(re.search("you",scene)) 将不起作用。请改用match_index = re.search("you",scene)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2016-11-08
    • 2014-04-26
    • 2021-01-15
    • 2020-03-09
    • 2013-05-14
    相关资源
    最近更新 更多