views:

157

answers:

3

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

+13  A: 

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)
unutbu
+1. Elegant and Pythonic.
Manoj Govindan
`dateutil` is definitely the cleanest.
Oli
+4  A: 

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

Manoj Govindan
Your second solution wouldn't work too well if `date_str` was, say, `25th November 2010` -- that's why in my A I'm using a RE, rather than plain string slicing, to strip the ordinal suffix;-).
Alex Martelli
@Alex: You are correct. Some kind of matching/cleanup *is* required.
Manoj Govindan
+5  A: 

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.

Alex Martelli