[Tutor] Hi, First question
Alexander
rhettnaxel at gmail.com
Sun Jun 16 17:40:30 CEST 2013
On Sat, Jun 15, 2013 at 1:22 AM, Patrick Williams <pdw0005 at gmail.com> wrote:
> 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.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
Please include the "mbox-short.txt" file. Also you should include the
output of your code (copy and paste it).
--
Alexander
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130616/5f0e5c0f/attachment.html>
More information about the Tutor
mailing list