Python 属性错误:';非类型';对象没有属性';启动';数据营演习

Python 属性错误:';非类型';对象没有属性';启动';数据营演习,python,nlp,ipython,attributeerror,nonetype,Python,Nlp,Ipython,Attributeerror,Nonetype,我正在做Python中NLP的数据营课程。其中一个练习要求如下: 使用re.search()搜索场景1中第一个出现的单词“coconuts”。将结果存储在match中。分别使用其.start()和.end()方法打印匹配的开始索引和结束索引 我正在这样做: match = re.search("coconuts", scene_one) print(match.start(), match.end()) print(match) 它似乎在iPython shell中正常工作,但当我在Jup

我正在做Python中NLP的数据营课程。其中一个练习要求如下: 使用re.search()搜索场景1中第一个出现的单词“coconuts”。将结果存储在match中。分别使用其.start()和.end()方法打印匹配的开始索引和结束索引

我正在这样做:

match = re.search("coconuts", scene_one)

print(match.start(), match.end())

print(match)
它似乎在iPython shell中正常工作,但当我在Jupyter笔记本上运行代码时,会收到以下错误消息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-dbcdf93b0e90> in <module>
----> 1 print(match.start(), match.end())

AttributeError: 'NoneType' object has no attribute 'start'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
---->1个打印(match.start(),match.end())
AttributeError:“非类型”对象没有属性“开始”

这意味着什么?

在调用匹配对象上的任何方法之前,首先必须检查匹配对象是否存在:

match = re.search("coconuts", scene_one)
if match is not None:
   print(match.start(), match.end())
   print(match)

re.search()
如果字符串中没有与模式匹配的位置,将“返回
None
scene\u one
在iPython和Jupyter中是否相同?当使用
re.search
的结果时,假设成功不是一个好主意。首先测试
None