You can try to "reduce" the string as long as you can't decode it:
from dateutil import parser
def reduce_string(string):
i = len(string) - 1
while string[i] >= '0' and string[i] < '9':
i -= 1
while string[i] < '0' or string[i] > '9':
i -= 1
return string[:i + 1]
def find_date(string):
while string:
try:
dateString = parser.parse(string, fuzzy=True)
year = dateString.year
month = dateString.month
day = dateString.day
return (year, month, day)
except ValueError:
pass
string = reduce_string(string)
return None
date = find_date('2007-09-10_00005')
if date:
print date
else:
print "can't decode"
The idea is to removing the end of the string (any numbers then any non-numbers) until the parser can decode it to a valid date.