python3中re模块的finditer()函数报出AttributeError: 'callable_iterator' object has no attribute 'next'的错误

>>> s = 'This and that.'
>>>> re.finditer(r'(th\w+) and (th\w+)',s,re.I).next().groups()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'callable_iterator' object has no attribute 'next'

报错原因是python版本的问题,修改办法如下:

>>> s = 'This and that.'
>>> res = re.finditer(r'(th\w+) and (th\w+)',str,re.I)#注意这个地方的版本问题
>>> print(next(res).group(2))
that
>>> res = re.finditer(r'(th\w+) and (th\w+)',str,re.I)#注意这个地方的版本问题
>>> print(next(res).groups())
('this', 'that')
>>> res = re.finditer(r'(th\w+) and (th\w+)',str,re.I)#注意这个地方的版本问题
>>> print(next(res).group(1))
this
>>> res = re.finditer(r'(th\w+) and (th\w+)',str,re.I)#注意这个地方的版本问题
>>> print(next(res).group(0))
this and that

补充:字符串前面加上 r 表示原生字符串(rawstring)




在这里插入图片描述

知乎:叁贰壹

简书:带只拖鞋去流浪

关注我,带你一起写bug

warning :未经授权,不得转载

有问题的小伙伴请在下方留言,喜欢就点个赞吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章