I think you might be slightly better off doing something similiar to what @mgilson said, but with an if
statement rather than try/catch
, since you may accidentally catch errors you didn't mean to.
My understanding is that the error you're looking to catch here is when the date field is blank, so that's what I'm going with.
def parse_datetime(warrant_issued):
# Using Python's "truthiness" to take care of both '' and None, however it comes out
if warrant_issued:
warrant_issued_no = datetime.strptime(warrant_issued, '%m/%d/%Y')
else:
warrant_issued_no = ''
warrants_issued = [parse_datetime(cell.get_text().strip()) for cell in cells]
This way, if you end up getting some other error that still throws a ValueError
but isn't from there not being a date, it'll throw an exception and you'll be able to take care of it.