Parsing a string into a datetime object

MRAB google at
Sat Jan 24 19:50:04 CET 2009

Mark.Petrovic wrote:
 > Good day.
 > Might someone comment on why %f is not accepted as a valid field
 > directive in:
 >>>> from datetime import datetime
 >>>> created="2009-01-24 16:04:55.882788"
 >>>> dt = datetime.strptime(created,"%Y-%m-%d %H:%M:%S.%f")
 > Traceback (most recent call last):
 >   File "<stdin>", line 1, in <module>
 >   File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/
 > python2.5/", line 321, in strptime
 >     (bad_directive, format))
 > ValueError: 'f' is a bad directive in format '%Y-%m-%d %H:%M:%S.%f'
 > This is for Python 2.5.1 under OS X.
 > Thank you.
I believe that Python simply uses the 'strptime' (or equivalent)
function in the underlying C library: if that doesn't accept %f then
neither does Python.

More information about the Python-list mailing list