[Tutor] Need help with dates in Python
James Reynolds
eire1130 at gmail.com
Wed Mar 9 21:56:40 CET 2011
On Wed, Mar 9, 2011 at 1:34 PM, nookasree ponamala <nookasree at yahoo.com>wrote:
> 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
> >
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
Other than some of the previous touched upon commentary, and going slightly
beyond your question of "why doesn't this work" you can parse text to
datetime objects directly in python by using the datetime.datetime.strptime
function. In your case, it would look something like this: dt1 =
datetime.datetime.strptime(dt1, '%Y-%m-%d'). This part, '%Y-%m-%d', is
telling python how you should expect to see the datetimes in text version (4
digit year, 2 digit month, 2 digit day separated by dashes). You can then do
comparisons directly the datetime object.
Personally though, taking this to the next level, I would represent each
line from the date you care about as a class, if you are familiar with how
to use classes that is, with soemthing that looks like this:
class id:
>
> def __init__(self,sno,dt1,amt,id1,id2):
>
> self.sno = sno
> self.dt1 = datetime.datetime.strptime(dt1, '%Y-%m-%d')
> self.amt = amt
> self.id1 = id1
> self.id2 = id2
>
> def print_all(self):
> print self.sno, self.dt1, self.amt, self.id1, self.id2
Even if you aren't familiar with how to use classes, I
would definitely consider using strptime instead of all those splits and
whatnot.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110309/9bc38389/attachment.html>
More information about the Tutor
mailing list