[Tutor] calculate the sum of a variable - python

nookasree ponamala nookasree at yahoo.com
Mon Mar 7 04:31:53 CET 2011


Hi :

I'm a Senior SAS Analyst. I'm trying to learn Python. I would appreciate if anybody could help me with this. It works fine if I give input  instead of reading a text file. I don't understand where I'm going wrong.

I'm trying to read a text file and find out the following:
1. Sum of amt for each id
2. Count of id 
3. minimum of date1
4. maximum of date1

Here is the sample text file:

test.txt file:

bin1	cd1	date1	amt	cd    id cd2
452  2       2010-02-20      $23.26  0	  8100059542        06107
452  2       2010-02-20      $20.78  0          8100059542        06107
452  2       2010-02-24      $5.99   2          8100839745        20151
452  2       2010-02-12      $114.25 7          8100839745        98101
452  2       2010-02-06      $28.00  0          8101142362        06032
452  2       2010-02-09      $15.01  0          8100274453        06040
452  18      2010-02-13      $113.24 0          8100274453        06040
452  2       2010-02-13      $31.80  0          8100274453        06040


Here is the code I've tried out to calculate sum of amt by id:

import sys
from itertools import groupby
from operator import itemgetter
t = ()
tot = []
for line in open ('test.txt','r'):
	aline = line.rstrip().split()
	a = aline[5]
	b = (aline[3].strip('$'))
	t = (a,b)
	t1 = str(t)
	tot.append(t1)
	print tot
def summary(data, key=itemgetter(0), value=itemgetter(1)):   
	for k, group in groupby(data, key):
		yield (k, sum(value(row) for row in group))

if __name__ == "__main__":	
	for id, tot_spend in summary(tot, key=itemgetter(0), value=itemgetter(1)):
	    print id, tot_spend


Error:
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 3, in summary
TypeError: unsupported operand type(s) for +: 'int' and 'str'


Thanks,
Sree.


      


More information about the Tutor mailing list