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