python datetime strptime wildcard

前端 未结 5 2022
有刺的猬
有刺的猬 2021-01-04 02:23

I want to parse dates like these into a datetime object:

  • December 12th, 2008
  • January 1st, 2009

The following will work for the first d

5条回答
  •  情歌与酒
    2021-01-04 02:49

    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)
    

提交回复
热议问题