开发者

Parse this date in Python: 5th November 2010

开发者 https://www.devze.com 2023-01-11 07:51 出处:网络
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):

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 2开发者_如何学编程010


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)


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号