0

I am having trouble with solving and assignment in which the question asks: "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 a variable named sum in your solution. You can download the sample data at http://www.pythonlearn.com/code/mbox-short.txt when you are testing below enter mbox-short.txt as the file name."

my code is:

# Use the file name mbox-short.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
count = 0
total = 0

#counting lines
for line in fh:
    line = line.rstrip()
    if not line.startswith("X-DSPAM-Confidence:") : 
        continue
    count = count+1
fcount=float(count)
print fcount


# total spam number thing
for line in fh:
    if line.startswith("X-DSPAM-Confidence:"):
        pos = text.find(':')
        slice = text[pos+1:]
        fslice = float(slice)
        total = fslice + total
ftotal = float(total)
print ftotal

#average
print "Average spam confidence:", (ftotal / fcount)

This code produces the output: 27.0, 0.0, Average Spam Confidence: 0.0

When my code runs, the value for total never increases, so there must be a problem with extracting the strings of numbers after the colon, but instead of getting an error, I am receiving a 0 value. I have run the code block before on a previous assignment in which I was required to extract that floating point value from a string input of that form, so I am guessing the error is coming from how I call for the line from the file.

How can I make this run properly to sum up the floating point values?

Thank you

jboges
  • 141
  • 1
  • 10
  • 2
    You've exhausted the file-handler, you can't iterate over it twice without either using `seek` to get back to the beginning or better yet, using `with` blocks to open your file every time you want to iterate over it – juanpa.arrivillaga May 03 '17 at 17:50
  • I'm sorry but the course i'm taking has not progressed that far, so I dont know what with or seek means. Are you saying I need to incorporate my second block where I find the total of the spam values into the first for statement? Also, why would this return a 0 value for the total? – jboges May 03 '17 at 18:09
  • 1
    File-handlers have a `seek` method, which allows you to move the file-cursor to some specific offset (in bytes) in the file. Once you've iterated over your file-handler once, it is at the last byte, say byte 1200. If you try to iterate over it again, it won't go anywhere and your loop body won't execute. If you use `fh.seek(0)` it goes back to the beginning, and you can loop again. But you really shouldn't be dealing at this low of a level for everyday stuff. Instead, use a context manager, i.e. a `with` block, to work with files, which will automatically close your file when it's done to. – juanpa.arrivillaga May 03 '17 at 18:20
  • Ok thank you that makes sense now. I solved my problem by incorporating the two If statements into the same for block before the file-handler moves to the last byte. – jboges May 03 '17 at 18:30
  • 1
    Yeah, you definitely should only be doing one pass anyway. But regardless, you should learn to use `with` to open files. It's just a good habit to get into. – juanpa.arrivillaga May 03 '17 at 18:34

0 Answers0