python datetime strptime wildcard

2020-03-01 06:17发布

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?

5条回答
小情绪 Triste *
2楼-- · 2020-03-01 06:35

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
查看更多
Fickle 薄情
3楼-- · 2020-03-01 06:43

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

查看更多
姐就是有狂的资本
4楼-- · 2020-03-01 06:46

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)
>>> 
查看更多
家丑人穷心不美
5楼-- · 2020-03-01 06:49

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")
查看更多
The star\"
6楼-- · 2020-03-01 06:58

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)
查看更多
登录 后发表回答