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 7717181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:49:18+00:00 2026-06-01T02:49:18+00:00

I have this so far but I don’t know how to write over the

  • 0

I have this so far but I don’t know how to write over the .dat file:

def main():
   fname = input("Enter filename:")
   infile = open(fname, "r")
   data = infile.read()
   print(data)

for line in infile.readlines():
    score = int(line)
    counts[score] = counts[score]+1
    infile.close()

total=0
for c in enumerate(counts):
    total = total + i*c
    average = float(total)/float(sum(counts))
print(average) 
main()

Here is my .dat file:

4
3
5
6
7

My statistics professor expects us to learn Python to compute the mean and standard deviation. All I need to know is how to do the mean and then I’ve got the rest figured out. I want to know how does Python write over each line in a .dat file. Could someone tell me how to fix this code? I’ve never done programming before.

  • 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-01T02:49:20+00:00Added an answer on June 1, 2026 at 2:49 am

    To answer your question, as I understand it, in three parts:

    How to read the file in

    in your example you use

    infile.read()

    which reads the entire contents of the file into a string and takes you to the end of file. Therefore the following

    infile.readlines()

    will read nothing more. You should omit the first read().

    How to compute the mean

    There are many ways to do this in python – more or less elegant – and also I guess it depends on exactly what the problem is. But in the simplest case you can just sum and count the values as you go , then divide sum by count at the end to get the result:

    infile = open("d.dat", "r")
    total = 0.0
    count = 0
    for line in infile.readlines():    
        print ("reading in line: ",line)
        try:
            line_value = float(line)
            total += line_value
            count += 1
            print ("value = ",line_value, "running total =",total, "valid lines read = ",count)
        except:
            pass #skipping non-numeric lines or characters
    
    infile.close()
    

    The try/except part is just in case you have lines or characters in the file that can’t be turned into floats, these will be skipped.

    How to write to the .dat file

    Finally you seem to be asking how to write the result back out to the d.dat file. Not sure whether you really need to do this, it should be acceptable to just display the result as in the above code. However if you do need to write it back to the same file, just close it after reading from it, reopen it for writing (in ‘append’ mode so output goes to the end of the file), and output the result using write().

    outfile = open("d.dat","a")
    outfile.write("\naverage = final total / number of data points = " + str(total/count)+"\n")
    outfile.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem and don't know where to start. I need to write
I have this code so far which perfectly but relies on there being a
Ok, this is what I have so far - but it gives me an
I am pretty new to this iPhone dev thing but so far I have
This has / may have been asked before, but, as far as I remember,
This is a very lame question, but I have struggled with it far too
I have this particular scenario, which is simple really, but I don't see (so
Thanks to everyone out there helping newbies like me. So far I have this:
I have used this effect before, everything is in order (As far as I
I have had no luck with this task so far so grateful for any

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.