[Tutor] Need help with dates in Python

nookasree ponamala nookasree at yahoo.com
Wed Mar 9 19:34:42 CET 2011


Hi,
I'm new to Python programming. I've changed the code to below, but still it is not working, Could you pls. make the corrections in my code.

import datetime
t = ()
tot = []
min = datetime.date(2008, 1, 1)
max = datetime.date(2012, 12, 31)
for line in open ('test2.txt','r'):
	data = line.rstrip().split()
	a = data[9]
	b = data[4]
	(year, month, day) = b.split('-')
	year = int(year)
	month = int(month)
	day = int(day)
	t = (year,month,day)
		if t < max:
		maxyr = max
		if t > min:
		minyr = min
		t = (a,b,maxyr,minyr)
		tot.append(t)
		print t

Thanks
Sree.

--- On Wed, 3/9/11, Andre Engels <andreengels at gmail.com> wrote:

> From: Andre Engels <andreengels at gmail.com>
> Subject: Re: [Tutor] Need help with dates in Python
> To: "nookasree ponamala" <nookasree at yahoo.com>
> Cc: tutor at python.org
> Date: Wednesday, March 9, 2011, 2:16 PM
> On Wed, Mar 9, 2011 at 9:21 AM,
> nookasree ponamala <nookasree at yahoo.com>
> wrote:
> > Hi,
> >
> > I need help in finding the minimum date and maximum
> date in a file.
> > Here is my test file:
> > s.no:   dt1     amt     id1     id2
> > 452     2010-02-20      $23.26      059542  
>      06107
> > 452     2010-02-05      $20.78      059542  
>      06107
> > 451     2010-02-24      $5.99       059542  
>      20151
> > 452     2010-02-12      $114.25     839745  
>      98101
> > 452     2010-02-06      $28.00      839745  
>      06032
> > 451     2010-02-12      $57.00      839745  
>      06269
> >
> > I want to get the minimum and maximum dt1 for each
> id1
> >
> > Required result:
> >
> > id1 mindate maxdate
> > 059542  2010-02-24      2010-02-20
> > 839745  2010-02-06      2010-02-12
> >
> > Code: The code I tried. It doesn't work though.
> >
> > import sys
> > import os
> > t = ()
> > tot = []
> > maxyr = 2012
> > minyr = 2008
> > maxday = 31
> > minday = 1
> > maxmon = 12
> > minmon = 1
> >
> > for line in open ('test2.txt','r'):
> >        data = line.rstrip().split()
> >        a = data[3]
> >        b = data[1]
> >        (year, month, day) = b.split('-')
> >        year = int(year)
> >        month = int(month)
> >        day = int(day)
> > if year > maxyr:
> >        maxyr = year
> > elif year < minyr:
> >        minyr = year
> > if month > maxmon:
> >        maxmon = month
> >        elif month < minmon:
> >        minmon = month
> >        if day > maxday:
> >        maxday = day
> >        elif day < minday:
> >        minday = day
> >        max = (maxyr,maxmon,maxday)
> >        min = (minyr,minmon,minday)
> >        t = (a,b,max,min)
> >        tot.append(t)
> >        print t
> >
> > Could you pls. help me with this.
> 
> I see several things go wrong. Here a list, which may well
> not be complete:
> 
> * You want the mindate and maxdate for each id1, but you
> remember only
> a single minyr, maxyr etcetera. There's no way that that is
> going to
> work.
> * You initialize minyr etcetera to a date before the first
> date you
> will see, nd maxyr etcetera to a date after the last date.
> This means
> that you will never find an earlier respectively later one,
> so they
> would never be changed. You should do it exactly the other
> way around
> - minyr etcetera should be _later_ than any date that may
> occur, maxyr
> etcetera _earlier_.
> * You move "if year > maxyr" back to the left. This
> means that it is
> not part of the loop, but is executed (only) once _after_
> the loop has
> been gone through
> * year < minyear should be "if", not "elif": it is
> possible that the
> new date is both the first _and_ the last date that has
> been found
> (this will be the case with the first date)
> * You change maxyear, maxmonth and maxday independently.
> That is not
> what you are trying to do - you want the last date, not the
> highest
> year, highest month and highest day (if the dates were
> 2001-12-01,
> 2011-11-03 and 2005-05-30, you want the maximum date to be
> 2011-11-03,
> not 2011-12-30). You should thus find a way to compare the
> *complete
> date* and then if it is later than the maxdate or earlier
> than the
> mindate change the *complete date*
> * At the end you show (well, in this case you don't because
> it is
> under "if month > maxmon") a quadruple consisting of
> id1, current
> date, lowest date and highest date - EACH time. You want
> only the
> triple and only after the last date of some value of id1
> has been
> parsed (best to do that after all lines have been parsed)
> * The code as shown will lead to a syntax error anyway
> because you did
> not indent extra after "elif month < minmon:", "if day
> > maxday:" and
> "elif day < minday:".
> 
> 
> 
> 
> -- 
> André Engels, andreengels at gmail.com
> 


      


More information about the Tutor mailing list