Parse this date in Python: 5th November 2010

2019-01-25 22:40发布

问题:

I'm having a bad time with date parsing and formatting today.

Points for somebody who can parse this date format into a datetime.date or datetime.datetime (I'm not too fussy but I'd prefer .date):

5th November 2010

回答1:

Using dateutil:

In [2]: import dateutil.parser as dparser

In [3]: date = dparser.parse('5th November 2010')

In [4]: date
Out[4]: datetime.datetime(2010, 11, 5, 0, 0)


回答2:

Unfortunately, strptime has no format characters for "skip an ordinal suffix" -- so, I'd do the skipping first, with a little RE, and then parse the resulting "clear" string. I.e.:

>>> import re
>>> import datetime
>>> ordn = re.compile(r'(?<=\d)(st|nd|rd|th)\b')
>>> def parse(s):
...   cleans = ordn.sub('', s)
...   dt = datetime.datetime.strptime(cleans, '%d %B %Y')
...   return dt.date()
... 
>>> parse('5th November 2010')
datetime.date(2010, 11, 5)

Your preference for date vs datetime is no problem of course, that's what the .date() method of datetime objects is for;-).

Third-party extensions like dateutil can be useful if you need to do a lot of "fuzzy" date parsing (or other fancy date-related stuff;-), by the way.



回答3:

If the ordinal is constant then:

datetime.strptime(s, '%dth %B %Y')

Else:

date_str = '5th November 2010'
modified_date_str = date_str[0:1] + date_str[3:]
datetime.strptime(modified_date_str, '%d %B %Y')

Or like ~unutbu said use dateutil :)