[Tutor] Hi, First question

Patrick Williams pdw0005 at gmail.com
Sat Jun 15 07:22:51 CEST 2013


Hi so I am making a bit of code to extract a bit of numbers data from a
file and then find the average of that data, however while I can get the
code to extract each specific piece of data I need, I can't seem to get the
numbers to add separately  so I can get a proper average. My sum1 variable
seems to only take the last bit of data entered. I was just wondering if
anyone knows what I'm doing wrong, the course I'm following hadn't started
using regex (or even proper lists) at this point, so there must be a way to
do it without. here's the code. the average of the data should be 0.6789 or
something, but I get 0.0334343 or something.

count=0
lst=list()
fname='mbox-short.txt'
fhand=open(fname)
for line in fhand:
    if line.startswith('X-DSPAM-Confidence:'):
        count=count+1
        colpos=line.find(':')
        zpos=line.find('0',colpos)
        num=float(line[zpos:50])
        sum1=0+num
        avg=float(sum1)/int(count)
print 'Count-', count,'--', 'Average-', avg

Any help at all is appreciated, and thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130615/a150abc6/attachment-0001.html>


More information about the Tutor mailing list