I want to parse dates like these into a datetime object:
- December 12th, 2008
- January 1st, 2009
The following will work for the first date:
datetime.strptime("December 12th, 2008", "%B %dth, %Y")
but will fail for the second because of the suffix to the day number ('st'). So, is there an undocumented wildcard character in strptime? Or a better approach altogether?
For anyone who, like me, just want something that "works" without an additional module, this is a quick and dirty solution.
Try using the dateutil.parser module.
Additional documentation can be found here: http://labix.org/python-dateutil
You need Gustavo Niemeyer's python_dateutil -- once it's installed,
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:
If you want to use arbitrary wildcards, you can use datetime-glob, a module we developed to parse date/times from a list of files generated by a consistent date/time formatting. From the module's documentation: