Python 3+ help needed: search for multiple RegEx substrings
It may be newbe question, but I'm stuck:
I've got a SQLite table with sells records - where in field 13 located
shipping prices - there are essentially 3 possibilities:
Price: for ex. £15.20
free
not specified
Problem is there is not always only these words: for ex. it can say
"shipping is £15.20" or "shipping free" - I need to normalize it to the
aforementioned possibilities. I use RegEx:
def correct_shipping(db_data):
pattern=re.compile("\£(\d+.\d+)") #search for price
pattern_free=re.compile("free") #search for free shipping
pattern_not=re.compile("not specified") #search for shipping not specified
for every_line in db_data:
try:
found=pattern.search(every_line[13].replace(',','')).group(1)
except:
try:
found=pattern_free.search(every_line[13]).group()
except:
found=pattern_not.search(every_line[13]).group()
if found:
query="UPDATE MAINTABLE SET Shipping='"+found+"' WHERE
Id="+str(every_line[0])
db_cursor.execute(query)
db_connection.commit()
But this code is raising exception
AttributeError: 'NoneType' object has no attribute 'group'
- first result in form "5.20" trigger it because none of patterns is found.
Question is how to properly search for string (is try/except is necessary
at all ?) or how just ignore exception if none of the strings is found
(this is not so good solution though ?)
Thank You in advance
No comments:
Post a Comment