A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Jadzia

  • one year ago

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.

  • This Question is Closed
  1. Jadzia
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

  2. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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.

  3. Jadzia
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I tried doing the splitting first, but it says "list index out of range"

  4. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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.

  5. Jadzia
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes, that's what I did...

    1 Attachment
  6. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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] ```

  7. Jadzia
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    oh okay! Now, I just did that and now it says "bad input"

  8. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Bad input happens when indentation is not right in python... make sure you are using right indentation

  9. Jadzia
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    okay. I'm just wondering if it is okay to put it before the 'continue'?

  10. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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"

  11. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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 ```

  12. Jadzia
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks! I just have to adjust the average but I can manage it from here. Thank you so much

  13. CyberShadow
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Glad to help :)

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.