Ad

Python Code To Read File And Extract Data

- 1 answer

I am working on a small code assignment and stumbled upon something that I don't understand. So I am hoping someone can help me and give me an explanation.

So I am working on a code to write a program that prompts for a file name. The file can be found here: http://www.pythonlearn.com/code/mbox-short.txt. Using that file, I read through it and look for lines of the form:

X-DSPAM-Confidence:    0.8475. 

My job is to count these lines and extract the floating point values from each of the lines and compute the average of those values.

I have the following code so far:

#fname = File Path
fh = open(fname)
emptyl=[]
for line in fh:

    if not line.startswith("X-DSPAM-Confidence:"): continue
    line=line.strip()
    print line

    for confidence in line:
        try:
            x = float(line[-6:])
            print x
            y = emptyl.append(x)
            print y
        except:
            pass

When I print Line, I get 27 lines with X-DSPAM-Confidence:". However when I print x to see the result of my code, I get each number that shows up in print line repeated 26 times. Why are the numbers repeating? Also when I print y, I get None. I also don't understand why its giving me None instead of an actual number.

Ad

Answer

Get rid of this extra loop for confidence in line::

emptyl=[]
with open(fname) as fh:
    for line in fh:
        if not line.startswith("X-DSPAM-Confidence:"): 
            continue
        line = line.strip()
        try:
            x = float(line[-6:])
            emptyl.append(x)
        except Value Error:
            pass

You have two nested loops. First you loop through all lines:

for line in fh:

and for each line you loop through all characters in this line:

  for confidence in line:

and append this number: float(line[-6:]) again and again.

append() is a method that modifies the object it belongs to. So:

emptyl.append(x)

modifies emptyl. The return value of this action is None. Typically, it is not used because the action we are up to is modifying emptyl.

Ad
source: stackoverflow.com
Ad