[Tutor] Need help with dates in Python

C.Y. Ruhulessin izzaddin.ruhulessin at gmail.com
Wed Mar 9 09:29:03 CET 2011


import datetime

min = datetime.date(2008, 1, 1)
max = datetime.date(2012, 12, 31)

file = open ('test2.txt','r')
line = file.readline()[-1]
while line:
   # your code


2011/3/9 nookasree ponamala <nookasree at yahoo.com>

> 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.
>
> Thanks
> Sree.
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110309/c09538dc/attachment-0001.html>


More information about the Tutor mailing list