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 :未經授權,不得轉載

有問題的小夥伴請在下方留言,喜歡就點個贊吧

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