Bug in strptime?

H

Henk-Jan de Jong

Hello all,

I'm completely new in Python and have the next problem. I have a huge file
with dates a want to convert from one format to another.

e.g 31-12-2003 should become 31122003. I'm using the next piece of code:


def changeDate(inDate, inFormat, outFormat):
return strftime(outFormat, strptime(inDate, inFormat))

#the next part inside the loop reading the file:
.. .
newDate = changeDate(oldDate, "%d-%m-%Y", "%d%m%Y")
.. .

When the file is small (less then 50 lines or so), it works fine. If the
file gets bigger I get the next error:

File "C:\Python23\lib\_strptime.py", line 424, in strptime
raise ValueError("time data did not match format: data=%s fmt=%s" %
ValueError: time data did not match format: data= fmt=%d-%m-%Y

What can be wrong?? The data seems ok.

Greetinx,

Matuka
 
J

John Roth

Henk-Jan de Jong said:
Hello all,

I'm completely new in Python and have the next problem. I have a huge file
with dates a want to convert from one format to another.

e.g 31-12-2003 should become 31122003. I'm using the next piece of code:


def changeDate(inDate, inFormat, outFormat):
return strftime(outFormat, strptime(inDate, inFormat))

#the next part inside the loop reading the file:
. .
newDate = changeDate(oldDate, "%d-%m-%Y", "%d%m%Y")
. .

When the file is small (less then 50 lines or so), it works fine. If the
file gets bigger I get the next error:

File "C:\Python23\lib\_strptime.py", line 424, in strptime
raise ValueError("time data did not match format: data=%s fmt=%s" %
ValueError: time data did not match format: data= fmt=%d-%m-%Y

What can be wrong?? The data seems ok.

Put a try block around the line causing the error, and print out the
input line at that point.

There is a high probability that enlightenment will occur...

John Roth
 
H

Henk-Jan de Jong

John,

good advice! I figured out it's not strptime but some feature in the
linesplitter I wrote :(

Thanx

Matuka
 
T

Tim Roberts

Henk-Jan de Jong said:
I'm completely new in Python and have the next problem. I have a huge file
with dates a want to convert from one format to another.

e.g 31-12-2003 should become 31122003. I'm using the next piece of code:


def changeDate(inDate, inFormat, outFormat):
return strftime(outFormat, strptime(inDate, inFormat))

#the next part inside the loop reading the file:
. .
newDate = changeDate(oldDate, "%d-%m-%Y", "%d%m%Y")
. .

It probably won't help you know, but this is certainly not the way I would
have solved this problem. There isn't any particular reason why you should
go to the trouble to treat these as dates, since you aren't really using
the values. Thus, this would be a LOT quicker:

newDate = oldDate.replace('-','')
 
H

Henk-Jan de Jong

Hello Tim,

I'm trying to make a generic routine for date-conversions. In my example the
format changes from dd-mm-yyyy to ddmmyyyy. I'm doing lots of
data-conversions, so a next time I maybe want to convert ddmmyyyy to
yyyymmdd or whatever. Furtermore the conversion-rules are described in an
xml-file for automatic processing:

eg. xml-description of the field in infile:
..
..
<field format="%d-%m-%Y"></field>
..
..
xml-description of the field in oufile:
..
..
<field format="%Y%m%d"></field>
..
..

If there is less complex way to get it done, I'd be interested to see how. I
bought my first Python book last weekend, so I've a lot to learn :))

Greetinx

Matuka
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top