Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:33:29+00:00 2026-06-17T18:33:29+00:00

Write a program to prompt for a file name, and then read through the

  • 0

Write a program to prompt for a file name, and then read through the file and look for lines of the form:
X-DSPAM-Confidence: 0.8475
When you encounter a line that starts with “X-DSPAM-Confidence:” pull apart the line to extract the floating point number on the line. Count these lines and the compute the total of the spam confidence values from these lines. When you reach the end of the file, print out the average spam confidence.

Enter the file name: mbox.txt
Average spam confidence: 0.894128046745

Enter the file name: mbox-short.txt
Average spam confidence: 0.750718518519
Test your file on the mbox.txt and mbox-short.txt files.

So far I have:

 fname = raw_input("Enter file name: ")
 fh = open(fname)
 for line in fh:
     pos  = fh.find(':0.750718518519')
     x = float(fh[pos:])
     print x

What is wrong with this code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:33:30+00:00Added an answer on June 17, 2026 at 6:33 pm

    It sounds like they’re asking you to average all the ‘X-DSPAM-Confidence’ numbers, rather than find 0.750718518519.

    Personally, I’d find the word you’re looking for, extract the number, then put all these numbers into a list and average them at the end.

    Something like this –

    # Get the filename from the user
    filename = raw_input("Enter file name: ")
    
    # An empty list to contain all our floats
    spamflts = []
    
    # Open the file to read ('r'), and loop through each line
    for line in open(filename, 'r'):
    
        # If the line starts with the text we want (with all whitespace stripped)
        if line.strip().startswith('X-DSPAM-Confidence'):
    
            # Then extract the number from the second half of the line
            # "text:number".split(':') will give you ['text', 'number']
            # So you use [1] to get the second half
            # Then we use .strip() to remove whitespace, and convert to a float
            flt = float(line.split(':')[1].strip())
    
            print flt
    
            # We then add the number to our list
            spamflts.append(flt)
    
    print spamflts
    # At the end of the loop, we work out the average - the sum divided by the length
    average = sum(spamflts)/len(spamflts)
    
    print average
    

    >>> lines = """X-DSPAM-Confidence: 1
    X-DSPAM-Confidence: 5
    Nothing on this line
    X-DSPAM-Confidence: 4"""
    
    >>> for line in lines.splitlines():
        print line
    
    
    X-DSPAM-Confidence: 1
    X-DSPAM-Confidence: 5
    Nothing on this line
    X-DSPAM-Confidence: 4
    

    Using find:

    >>> for line in lines.splitlines():
        pos = line.find('X-DSPAM-Confidence:')
        print pos
    
    0
    0
    -1
    0
    

    We can see that find() just gives us the position of 'X-DSPAM-Confidence:' in each line, not the position of the number after it.

    It’s easier to find if a line starts with 'X-DSPAM-Confidence:', then extract just the number like this:

    >>> for line in lines.splitlines():
        print line.startswith('X-DSPAM-Confidence')
    
    
    True
    True
    False
    True
    
    >>> for line in lines.splitlines():
        if line.startswith('X-DSPAM-Confidence'):
            print line.split(':')
    
    
    ['X-DSPAM-Confidence', ' 1']
    ['X-DSPAM-Confidence', ' 5']
    ['X-DSPAM-Confidence', ' 4']
    
    >>> for line in lines.splitlines():
        if line.startswith('X-DSPAM-Confidence'):
            print float(line.split(':')[1])
    
    
    1.0
    5.0
    4.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Within a VB.NET program, I want to read files from a filesystem, then write
My program has a function that read/write file from resource. This function I have
The task is to write a program which prompts for a filename and then
I am trying to write a program that will prompt you to enter someone's
I write a program to delete a file from somewhere of my harddisk in
Write a cryptographic program that works similarly to my crypto program:• Prompt the user
I am writing the batch file and executing it through C# program. Writing Batch
This is the question. write a program that prompts the user to input five
I'm willing to write program that after 5 sec shows text that was hidden
If I write program in python 2.7 and I want to run another script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.