time.strptime() and time.strftime() reciprocity

Discussion in 'Python' started by Greg Krohn, Feb 16, 2009.

  1. Greg Krohn

    Greg Krohn Guest

    [Apologies for the use of "reciprocity" in the subject - it's a little
    pompous, I know, but it fits so well. :)]

    Hello c.l.python!

    I'm trying to parse some dates of the form "01/29/09 12:55 PM" from a
    CSV file, but I'm having trouble with the format string in
    time.strptime() . In testing this I found that the time module chokes on
    a string that it created itself with the same format:

    ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
    Python 2.6.1 (r261:67515, Dec 5 2008, 13:58:38) [MSC v.1500 32 bit
    (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import time
    >>> fmt = "%m/%d/%y %I:%M %p"
    >>> print time.strptime(fmt, time.strftime(fmt))

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Python26\lib\_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
    File "C:\Python26\lib\_strptime.py", line 325, in _strptime
    (data_string, format))
    ValueError: time data '%m/%d/%y %I:%M %p' does not match format
    '02/16/09 04:52
    PM'

    So, yeah, that seems weird to me. Does anyone get similar results, know
    why this is happening, and/or how to fix it?

    Thanks.

    --
    ("%s@gmail com" % "Greg Krohn".lower()).replace(" ", ".")
    Greg Krohn, Feb 16, 2009
    #1
    1. Advertising

  2. Greg Krohn

    Denis Kasak Guest

    On Mon, Feb 16, 2009 at 11:57 PM, Greg Krohn <>
    wrote:

    <snip>

    > ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
    > Python 2.6.1 (r261:67515, Dec 5 2008, 13:58:38) [MSC v.1500 32 bit (Intel)]
    > on
    > win32
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> import time
    >>>> fmt = "%m/%d/%y %I:%M %p"
    >>>> print time.strptime(fmt, time.strftime(fmt))

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "C:\Python26\lib\_strptime.py", line 454, in _strptime_time
    > return _strptime(data_string, format)[0]
    > File "C:\Python26\lib\_strptime.py", line 325, in _strptime
    > (data_string, format))
    > ValueError: time data '%m/%d/%y %I:%M %p' does not match format '02/16/09
    > 04:52
    > PM'
    >
    > So, yeah, that seems weird to me. Does anyone get similar results, know why
    > this is happening, and/or how to fix it?


    It's actually pretty trivial to fix, which you would have also known
    if you had read the message of the exception more carefully. :p

    You passed the arguments for strptime() the wrong way around. Just
    pass them in reverse and your problem will be fixed.

    --
    Denis Kasak
    Denis Kasak, Feb 17, 2009
    #2
    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. Replies:
    0
    Views:
    651
  2. Maximilian Michel

    Threads and time.strptime()

    Maximilian Michel, Jul 3, 2006, in forum: Python
    Replies:
    4
    Views:
    333
    Maximilian Michel
    Jul 4, 2006
  3. kongkolvyu
    Replies:
    4
    Views:
    487
    Old Wolf
    Jan 4, 2006
  4. Replies:
    0
    Views:
    134
  5. Replies:
    0
    Views:
    139
Loading...

Share This Page