<br><br><div class="gmail_quote">On Wed, Mar 9, 2011 at 1:34 PM, nookasree ponamala <span dir="ltr"><<a href="mailto:nookasree@yahoo.com">nookasree@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi,<br>
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.<br>
<br>
import datetime<br>
t = ()<br>
tot = []<br>
<div class="im">min = datetime.date(2008, 1, 1)<br>
max = datetime.date(2012, 12, 31)<br>
</div><div class="im">for line in open ('test2.txt','r'):<br>
data = line.rstrip().split()<br>
</div> a = data[9]<br>
b = data[4]<br>
<div class="im"> (year, month, day) = b.split('-')<br>
year = int(year)<br>
month = int(month)<br>
day = int(day)<br>
</div> t = (year,month,day)<br>
if t < max:<br>
maxyr = max<br>
if t > min:<br>
minyr = min<br>
t = (a,b,maxyr,minyr)<br>
<div class="im"> tot.append(t)<br>
print t<br>
<br>
</div>Thanks<br>
Sree.<br>
<br>
--- On Wed, 3/9/11, Andre Engels <<a href="mailto:andreengels@gmail.com">andreengels@gmail.com</a>> wrote:<br>
<br>
> From: Andre Engels <<a href="mailto:andreengels@gmail.com">andreengels@gmail.com</a>><br>
> Subject: Re: [Tutor] Need help with dates in Python<br>
> To: "nookasree ponamala" <<a href="mailto:nookasree@yahoo.com">nookasree@yahoo.com</a>><br>
> Cc: <a href="mailto:tutor@python.org">tutor@python.org</a><br>
> Date: Wednesday, March 9, 2011, 2:16 PM<br>
<div><div></div><div class="h5">> On Wed, Mar 9, 2011 at 9:21 AM,<br>
> nookasree ponamala <<a href="mailto:nookasree@yahoo.com">nookasree@yahoo.com</a>><br>
> wrote:<br>
> > Hi,<br>
> ><br>
> > I need help in finding the minimum date and maximum<br>
> date in a file.<br>
> > Here is my test file:<br>
> > <a href="http://s.no" target="_blank">s.no</a>: dt1 amt id1 id2<br>
> > 452 2010-02-20 $23.26 059542 <br>
> 06107<br>
> > 452 2010-02-05 $20.78 059542 <br>
> 06107<br>
> > 451 2010-02-24 $5.99 059542 <br>
> 20151<br>
> > 452 2010-02-12 $114.25 839745 <br>
> 98101<br>
> > 452 2010-02-06 $28.00 839745 <br>
> 06032<br>
> > 451 2010-02-12 $57.00 839745 <br>
> 06269<br>
> ><br>
> > I want to get the minimum and maximum dt1 for each<br>
> id1<br>
> ><br>
> > Required result:<br>
> ><br>
> > id1 mindate maxdate<br>
> > 059542 2010-02-24 2010-02-20<br>
> > 839745 2010-02-06 2010-02-12<br>
> ><br>
> > Code: The code I tried. It doesn't work though.<br>
> ><br>
> > import sys<br>
> > import os<br>
> > t = ()<br>
> > tot = []<br>
> > maxyr = 2012<br>
> > minyr = 2008<br>
> > maxday = 31<br>
> > minday = 1<br>
> > maxmon = 12<br>
> > minmon = 1<br>
> ><br>
> > for line in open ('test2.txt','r'):<br>
> > data = line.rstrip().split()<br>
> > a = data[3]<br>
> > b = data[1]<br>
> > (year, month, day) = b.split('-')<br>
> > year = int(year)<br>
> > month = int(month)<br>
> > day = int(day)<br>
> > if year > maxyr:<br>
> > maxyr = year<br>
> > elif year < minyr:<br>
> > minyr = year<br>
> > if month > maxmon:<br>
> > maxmon = month<br>
> > elif month < minmon:<br>
> > minmon = month<br>
> > if day > maxday:<br>
> > maxday = day<br>
> > elif day < minday:<br>
> > minday = day<br>
> > max = (maxyr,maxmon,maxday)<br>
> > min = (minyr,minmon,minday)<br>
> > t = (a,b,max,min)<br>
> > tot.append(t)<br>
> > print t<br>
> ><br>
> > Could you pls. help me with this.<br>
><br>
> I see several things go wrong. Here a list, which may well<br>
> not be complete:<br>
><br>
> * You want the mindate and maxdate for each id1, but you<br>
> remember only<br>
> a single minyr, maxyr etcetera. There's no way that that is<br>
> going to<br>
> work.<br>
> * You initialize minyr etcetera to a date before the first<br>
> date you<br>
> will see, nd maxyr etcetera to a date after the last date.<br>
> This means<br>
> that you will never find an earlier respectively later one,<br>
> so they<br>
> would never be changed. You should do it exactly the other<br>
> way around<br>
> - minyr etcetera should be _later_ than any date that may<br>
> occur, maxyr<br>
> etcetera _earlier_.<br>
> * You move "if year > maxyr" back to the left. This<br>
> means that it is<br>
> not part of the loop, but is executed (only) once _after_<br>
> the loop has<br>
> been gone through<br>
> * year < minyear should be "if", not "elif": it is<br>
> possible that the<br>
> new date is both the first _and_ the last date that has<br>
> been found<br>
> (this will be the case with the first date)<br>
> * You change maxyear, maxmonth and maxday independently.<br>
> That is not<br>
> what you are trying to do - you want the last date, not the<br>
> highest<br>
> year, highest month and highest day (if the dates were<br>
> 2001-12-01,<br>
> 2011-11-03 and 2005-05-30, you want the maximum date to be<br>
> 2011-11-03,<br>
> not 2011-12-30). You should thus find a way to compare the<br>
> *complete<br>
> date* and then if it is later than the maxdate or earlier<br>
> than the<br>
> mindate change the *complete date*<br>
> * At the end you show (well, in this case you don't because<br>
> it is<br>
> under "if month > maxmon") a quadruple consisting of<br>
> id1, current<br>
> date, lowest date and highest date - EACH time. You want<br>
> only the<br>
> triple and only after the last date of some value of id1<br>
> has been<br>
> parsed (best to do that after all lines have been parsed)<br>
> * The code as shown will lead to a syntax error anyway<br>
> because you did<br>
> not indent extra after "elif month < minmon:", "if day<br>
> > maxday:" and<br>
> "elif day < minday:".<br>
><br>
><br>
><br>
><br>
> --<br>
> André Engels, <a href="mailto:andreengels@gmail.com">andreengels@gmail.com</a><br>
><br>
<br>
<br>
<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>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.</div>
<div><br></div><div>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:</div>
<div><br></div><div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
class id:<br> <br> def __init__(self,sno,dt1,amt,id1,id2):<br> <br> self.sno = sno<br> self.dt1 = datetime.datetime.strptime(dt1, '%Y-%m-%d')<br> self.amt = amt<br> self.id1 = id1<br>
self.id2 = id2<br> <br> def print_all(self):<br> print self.sno, self.dt1, self.amt, self.id1, self.id2</blockquote></div><div><br></div><div><br></div><div><br></div><div>Even if you aren't familiar with how to use classes, I would definitely consider using strptime instead of all those splits and whatnot.</div>
<div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>