python datetime strptime wildcard python datetime strptime wildcard python python

python datetime strptime wildcard


Try using the dateutil.parser module.

import dateutil.parserdate1 = dateutil.parser.parse("December 12th, 2008")date2 = dateutil.parser.parse("January 1st, 2009")

Additional documentation can be found here: http://labix.org/python-dateutil


You need Gustavo Niemeyer's python_dateutil -- once it's installed,

>>> from dateutil import parser>>> parser.parse('December 12th, 2008')datetime.datetime(2008, 12, 12, 0, 0)>>> parser.parse('January 1st, 2009')datetime.datetime(2009, 1, 1, 0, 0)>>> 


strptime is tricky because it relies on the underlying C library for its implementation, so some details differ between platforms. There doesn't seem to be a way to match the characters you need to. But you could clean the data first:

# Remove ordinal suffixes from numbers.date_in = re.sub(r"(st|nd|rd|th),", ",", date_in)# Parse the pure date.date = datetime.strptime(date_in, "%B %d, %Y")