Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Parsing a string into a datetime object

Reply
Thread Tools

Parsing a string into a datetime object

 
 
Mark.Petrovic
Guest
Posts: n/a
 
      01-24-2009
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.
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      01-24-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/_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.
 
Reply With Quote
 
 
 
 
Hrvoje Niksic
Guest
Posts: n/a
 
      01-24-2009
"Mark.Petrovic" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Mark.Petrovic
Guest
Posts: n/a
 
      01-25-2009
On Jan 24, 10:56*am, Hrvoje Niksic <(E-Mail Removed)> wrote:
> "Mark.Petrovic" <(E-Mail Removed)> 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime skip@pobox.com Python 2 01-06-2009 01:31 PM
[2.4.4] creating a datetime.datetime from an XML xs:dateTime Martin Python 0 12-27-2008 08:08 PM
mx.DateTime to datetime.datetime mp Python 1 07-28-2006 10:57 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments