[Tutor] Need help with dates in Python

nookasree ponamala nookasree at yahoo.com
Wed Mar 9 09:21:09 CET 2011


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.



      


More information about the Tutor mailing list