I would use try
here:
import datetime
def parse_date(line):
for template, length in [("[%H:%M:%S %d.%m.%Y]", 21),
("%H:%M:%S-%d.%m.%Y", 19), ...]:
try:
return datetime.datetime.strptime(line[:length], template)
except ValueError:
pass
This will work through all the templates, return
a datetime
object if it can extract one from the line
and return None
if none of the templates match.