Parsing a string into a datetime object

Discussion in 'Python' started by Mark.Petrovic, Jan 24, 2009.

  1. 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/_strptime.py", 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.
     
    Mark.Petrovic, Jan 24, 2009
    #1
    1. Advertising

  2. Mark.Petrovic

    MRAB Guest

    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/_strptime.py", 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.
     
    MRAB, Jan 24, 2009
    #2
    1. Advertising

  3. "Mark.Petrovic" <> writes:

    > 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/_strptime.py", 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.


    time.strptime is documented to use the same set of directives as
    time.strftime, and
    http://docs.python.org/library/time.html#time.strftime doesn't mention
    a %f directive.
     
    Hrvoje Niksic, Jan 24, 2009
    #3
  4. On Jan 24, 10:56 am, Hrvoje Niksic <> wrote:
    > "Mark.Petrovic" <> writes:
    > > 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/_strptime.py", 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.

    >
    > time.strptime is documented to use the same set of directives as
    > time.strftime, andhttp://docs.python.org/library/time.html#time.strftimedoesn't mention
    > a %f directive.


    Thank you for the timely reply.

    I guess I got mixed up by looking at the Python 2.6.1 docs, but used
    the Python 2.5.1 interpreter:

    http://docs.python.org/library/datetime.html

    wherein datetime.html does show the availability of the %f directive
    (if I'm reading all this correctly).
     
    Mark.Petrovic, Jan 25, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    711
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    537
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    392
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    338
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    762
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page