Parse this date in Python: 5th November 2010

徘徊边缘 提交于 2019-11-28 02:03:28

问题


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



来源:https://stackoverflow.com/questions/3461435/parse-this-date-in-python-5th-november-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!