Read Contents from URL


In [148]:
from urllib.request import urlopen

url_response = urlopen('http://www.py4inf.com/code/mbox-short.txt')
contents = str(url_response.read())

Split Contents Into Lines Using New-line ('\n')


In [149]:
lines = contents.split('\\n')

Slick Way


In [186]:
total = 0
count = 0
for line in lines:
    if line.startswith('X-DSPAM-Confidence'):
        (_, value) = line.split(':')
        total += float(value)
        count = count + 1

print(total/count)


0.7507185185185187

Slickest Way!


In [184]:
values = [float(line.split(':')[1]) for line in lines if line.startswith('X-DSPAM-Confidence')]

sum(values) / len(values)


Out[184]:
0.7507185185185187

In [ ]: