Jadzia
  • Jadzia
Python Help I'm not sure how to extract the numbers from the file. I'm sure that if I'll extract them, they will be considered as strings, so I also have to convert it to float. I just need help in extracting the numbers. The files are in the comments.
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
CyberShadow
  • CyberShadow
You can split line at ":" character to get the floating point value by using ``` floatString =line.split(":",1)[1] ``` Now this floatString will be a string so you can use ``` floatValue =float(floatString) ``` to parse the float value from string You can also execute both commands in a single line ``` floatVal = float(line.split(":",1)[1]) ``` Let me know if this doesnt work for you , or you have some doubt.
Jadzia
  • Jadzia
I tried doing the splitting first, but it says "list index out of range"

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

CyberShadow
  • CyberShadow
That would happen if there is no ":" in the line , make sure you use split after this line ``` if not line.startsWith("X-DSPAM-Confidence:") :continue floatString = line.split(":")[1] ``` dont add this splitting before that line.
Jadzia
  • Jadzia
yes, that's what I did...
1 Attachment
CyberShadow
  • CyberShadow
Why are you using 20 as index? When you'll split at ":" it will create an array of ["X-DSPAM-Confidence" , "whateverFloatValueYouHave"]. There are only two indexes 0 and 1.. Replace ``` floatString = line.split(":",1)[20] ``` with ``` floatString = line.split(":") [1] ```
Jadzia
  • Jadzia
oh okay! Now, I just did that and now it says "bad input"
CyberShadow
  • CyberShadow
Bad input happens when indentation is not right in python... make sure you are using right indentation
Jadzia
  • Jadzia
okay. I'm just wondering if it is okay to put it before the 'continue'?
CyberShadow
  • CyberShadow
No it's not.... we are checking if the line does not start with "X-DSPAM-Confidence:" then it would skip the rest of the code using Continue command. If we place it before continue , lines who doesnt contain any ":" will also call split() function and creates an array with single element , which would lead to "list index out of range errors again"
CyberShadow
  • CyberShadow
Following code should fulfil your requirement ``` fname = raw_input("Enter file name: ") fh = open (fname) count = 0 total = 0 for line in fh: line = line.rstrip() count = count +1 if not line.startswith("X-DSPAM-Confidence:"):continue floatVal = float(line.split(":",1)[1]) #print floatVal total = total + floatVal average = total/count print "Average spam confidence:",average ```
Jadzia
  • Jadzia
thanks! I just have to adjust the average but I can manage it from here. Thank you so much
CyberShadow
  • CyberShadow
Glad to help :)

Looking for something else?

Not the answer you are looking for? Search for more explanations.