473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Bug in strptime?

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
Jul 18 '05 #1
4 3864

"Henk-Jan de Jong" <he*****@xs4all.nl> wrote in message
news:3f***********************@news.xs4all.nl...
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
Greetinx,

Matuka

Jul 18 '05 #2
John,

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

Thanx

Matuka

"John Roth" <ne********@jhrothjr.com> wrote in message
news:vp************@news.supernews.com...

"Henk-Jan de Jong" <he*****@xs4all.nl> wrote in message
news:3f***********************@news.xs4all.nl...
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

Greetinx,

Matuka


Jul 18 '05 #3
"Henk-Jan de Jong" <he*****@xs4all.nl> wrote:

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('-','')
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #4
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

"Tim Roberts" <ti**@probo.com> wrote in message
news:t8********************************@4ax.com...
"Henk-Jan de Jong" <he*****@xs4all.nl> wrote:

I'm completely new in Python and have the next problem. I have a huge filewith 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('-','')
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gerrit Holl | last post by:
Hi, it seems the datetime library does not have a .strptime class method. If I want to create a datetime object from a string, I currently have to do: datetime.date(*time.strptime(s,...
4
by: Afanasiy | last post by:
Can I easily convert from ISO to Gregorian calendars in Python 2.3? For example, what Gregorian date is year 2004, week 1, day 1? If possible, I'd greatly prefer to do this with the standard...
1
by: Michele Simionato | last post by:
I have strings representing UTC dates and I want to convert them in time tuples using time.strptime. I have an issue with daylight savings. For instance Tue Sep 14 06:06:15 2004 is converted...
0
by: skip | last post by:
I'm looking for a solution (or ideas about a solution) to the problem that strftime(3) and strptime(3) don't understand time increments of less than one second. Most operating systems can provide...
2
by: Robert | last post by:
A certain DAV server reports a time stamp as "Tues, 30 Aug 2005 20:48:31" ( but not "Tue, ..." as usual) This produces the error below. >>> time.strptime("Tue, 30 Aug 2005 20:48:31","%a, %d...
4
by: kongkolvyu | last post by:
Hi The strptime function works just fine on Solaris. Here is an example on how I use it: struct tm tmpTm; if(strptime("20010101010101","%Y%m%d%H%M%S",&tmpTm)==NULL) printf("Error,String convert...
4
by: Maximilian Michel | last post by:
Hi all, I have an interesting problem: I have written code, that reads a logfile and parses it for date string (Thu Jun 29 14:01:23 2006). Standalone everthing works fine, all is recognized. ...
3
by: Tom Anderson | last post by:
Hello! Possibly i'm missing something really obvious here. But ... If i have a date-time string of the kind specified in RFC 1123, like this: Tue, 12 Aug 2008 20:48:59 -0700 Can i turn...
3
by: W. eWatson | last post by:
Apparently, use of strptime of datetime needs a workaround in Python 2.4 to work properly. The workaround is d = datetime.datetime(*(time.strptime(date_string, format))). However, when I try to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.