python datetime strptime wildcard

2020-03-01 05:51发布

问题:

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?

回答1:

Try using the dateutil.parser module.

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

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



回答2:

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)
>>> 


回答3:

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")


回答4:

For anyone who, like me, just want something that "works" without an additional module, this is a quick and dirty solution.

string_list = ["th", "rd", "nd", "st"]
time = None
for str in string_list:
    if (time is not None):
        break
    try:
        match_string = '%B %d' + str +', %Y'
        time = datetime.strptime("December 12th, 2008", match_string)
    except Exception:
        pass


回答5:

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:

>>> import datetime_glob
>>> matcher = datetime_glob.Matcher(
                         pattern='/some/path/*%Y-%m-%dT%H-%M-%SZ.jpg')

>>> matcher.match(path='/some/path/some-text2016-07-03T21-22-23Z.jpg')
datetime_glob.Match(year = 2016, month = 7, day = 3, 
                    hour = 21, minute = 22, second = 23, microsecond = None)

>>> match.as_datetime()
datetime.datetime(2016, 7, 3, 21, 22, 23)