lundi 22 mai 2017

Python: If statement "If not none" handling

I am using Python's regex with an if-statement: if the match is None, then it should go to the else clause. But it shows this error:

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

The script is:

import string
chars = re.escape(string.punctuation)
sub='FW: Re: 29699' 
if re.search("^FW: (\w{10})",sub).group(1) is not None :
    d=re.search("^FW: (\w{10})",sub).group(1)
else:
    a=re.sub(r'['+chars+']', ' ',sub)
    d='_'.join(a.split())

Every help is great help!

Aucun commentaire:

Enregistrer un commentaire