Created
July 5, 2016 03:59
-
-
Save zcakzwa/19a448416b9e7da3dc2096da72690914 to your computer and use it in GitHub Desktop.
7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: X-DSPAM-Confidence: 0.8475 Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Use the file name mbox-short.txt as the file name | |
| fname = raw_input("Enter file name: ") | |
| fh = open(fname) | |
| count = 0 | |
| total = 0 | |
| for line in fh: | |
| if not line.startswith("X-DSPAM-Confidence:") : continue | |
| t=line.find("0") | |
| number= float(line[t:]) | |
| count = count + 1 | |
| total = total + number | |
| average = total/count | |
| print "Average spam confidence:",average |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
guys use this:
fname = input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
t = line.find('0')
number = float(line[t:])
count = count + 1
total = total + number
else:
continue
average = total/count
print("Average spam confidence:", average)