[Tutor] calculate the sum of a variable - python
nookasree ponamala
nookasree at yahoo.com
Mon Mar 7 05:46:21 CET 2011
Thanks for the reply Wayne, but still it is not working,
when I used int It throws the below error:
File "<stdin>", line 2, in <module>
File "<stdin>", line 3, in summary
File "<stdin>", line 3, in <genexpr>
ValueError: invalid literal for int() with base 10: "'"
I tried using float and the error is:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 3, in summary
File "<stdin>", line 3, in <genexpr>
ValueError: invalid literal for float(): '
Thanks,
Sree.
--- On Mon, 3/7/11, Wayne Werner <waynejwerner at gmail.com> wrote:
From: Wayne Werner <waynejwerner at gmail.com>
Subject: Re: [Tutor] calculate the sum of a variable - python
To: "nookasree ponamala" <nookasree at yahoo.com>
Cc: tutor at python.org
Date: Monday, March 7, 2011, 9:14 AM
On Sun, Mar 6, 2011 at 9:31 PM, nookasree ponamala <nookasree at yahoo.com> wrote:
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'
Of course I first have to commend you for including the full traceback with the code because it makes this entirely easy to answer.
In general, the traceback tells you the most important stuff last, so I'll start with this line:
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
That tells us that the problem is you are trying to use + (addition) on an integer and a string - which you can't do because of the type mismatch (TypeError).
The next line
> File "<stdin>", line 3, in summary
tells us that the error occurred on line3 in summary:
1 | def summary(data, key=itemgetter(0), value=itemgetter(1)):
2 | for k, group in groupby(data, key):
3 | yield (k, sum(value(row) for row in group))
Well, there's no '+', but you do have 'sum', which uses addition under the hood. So how do you go about fixing it? Well, you change the value getting passed to sum to an integer (or other number):
sum(int(value(row)) for row in group)
Should either fix your problem, or throw a differen error if you try to convert a string like 'Hello' to an integer. (Alternatively, use float if you're interested in decimals)
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110306/0ef6bd76/attachment-0001.html>
More information about the Tutor
mailing list