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

G

Greg Krohn

[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.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.
 
D

Denis Kasak

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.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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top