How to find the average for a file then put it in another file

How to find the average for a file then put it in another file

how to calculate average from a text file in python
how to calculate average using for loop
write a program that computes and prints the average of the numbers in a text file
find average of numbers stored in sequential access file
how to add numbers in a text file python
how to find the average of 3 numbers in python
write a program to open the file and read the content to find the sum of all values in the file.
sum of numbers in file python

I want to find the average of the list inFile and then I would like to move it to the classscores.

classgrades.txt is:

Chapman 90 100 85 66 80 55    
Cleese 80 90 85 88    
Gilliam 78 82 80 80 75 77    
Idle 91    
Jones 68 90 22 100 0 80 85    
Palin 80 90 80 90

classcores.txt is empty

This is what I have so far... what should I do?

inFile = open('classgrades.txt','r')
outFile = open('classscores.txt','w')

for line in inFile:
  with open(r'classgrades.txt') as data:
    total_stuff = #Don't know what to do past here

biggest = min(total_stuff)
smallest = max(total_stuff)
print(biggest - smallest)

You will need to: - split each line by whitespace and slice out all items but the first - convert each string value in array to integer - sum all of those integer values in the array - add the sum for this line to total_sum - add the length of those values (the number of numbers) to total_numbers

However, this is only part of the problem... I will leave the rest up to you. This code will not write to the new file, it will simply take an average of all the numbers in the first file. If this isn't exactly what you are asking for, then try playing around with this stuff and you should be able to figure it all out.

inFile = open('classgrades.txt','r')
outFile = open('classscores.txt','w')
total_sum = 0
total_values = 0
with open(r'classgrades.txt') as inFile:
  for line in  inFile:
    # split by whitespace and slice out everything after 1st item
    num_strings = line.split(' ')[1:] 

    # convert each string value to an integer
    values = [int(n) for n in num_strings]

    # sum all the values on this line
    line_sum = sum(values)

    # add the sum of numbers in this line to the total_sum
    total_sum += line_sum

    # add the length of values in this line to total_values
    total_numbers += len(values)

average = total_sum // total_numbers  # // is integer division in python3
return average

Calculate the total from one file, output the average to another file , Create files named input.txt and output.txt and put them in C:. Fill in some integers in the input.txt for example as shown below. 23 12 33 10 4 6 44 31 7 50. I need to calculate the average from a list of numbers given on a text file. I got it to work but for some reason it is adding the last number in the list twice and I cant figure out why. Numbers in file are

you don't need to open file many times and you should close the files at the end of your program. Below is what I tried hope this works for you:

d1 = {}
with open(r'classgrades.txt','r') as fp:
    for line in fp:
        contents = line.strip().split(' ')
        # create mapping of student and his numbers
        d1[contents[0]] = map(int,contents[1:])

with open(r'classscores.txt','w') as fp:
    for key, item in d1.items():
        biggest = min(item)
        smallest = max(item)
        print(biggest - smallest)
        # average of all numbers
        avg = sum(item)/len(item)
        fp.write("%s %s\n"%(key,avg))

Averaging Numbers From a Text file in python without using sum , Write a program that prompts for a file name, then opens that file and reads from each of the lines and compute the average of those values and produce an output as shown below. I have had some help but I am not getting it. it was suggested to put the @Aric Introduce a new variable called num. Ok here is the basic perl script for calculating average. I assume you know how to run the script if you dont ( I will put up a tutorial later so for now dig around in other forms ) Perl Script for calculating average from another file #!/usr/bin/perl #calculate averate from a file use strict;…

Apologies if this is kind of advanced, I try to provide key words/phrases for you to search for to learn more.

Presuming you're looking for each student's separate average:

in_file = open('classgrades.txt', 'r')  # python naming style is under_score
out_file = open('classcores.txt', 'w')
all_grades = []  # if you want a class-wide average as well as individual averages

for line in in_file:
    # make a list of the things between spaces, like ["studentname", "90", "100"]
    student = line.split(' ')[0]

    # this next line includes "list comprehension" and "list slicing"
    # it gets every item in the list aside from the 0th index (student name),
    # and "casts" them to integers so we can do math on them.
    grades = [int(g) for g in line.split(' ')[1:]]

    # hanging on to every grade for later
    all_grades += grades  # lists can be +='d like numbers can

    average = int(sum(grades) / len(grades))

    # str.format() here is one way to do "string interpolation"
    out_file.write('{} {}\n'.format(student, average))

total_avg = sum(all_grades) / len(all_grades)
print('Class average: {}'.format(total_avg))

As others pointed out, it is good to get in the habit of closing files. Other responses here use with open() (as a "context manager") which is best practice because it automatically closes the file for you. To work with two files without having a data container in between (like Amit's d1 dictionary), you would do something like:

with open('in.txt') as in_file:
    with open('out.txt', 'w') as out_file:
        ... do things ...

Finding the average of numbers in a txt file, that read numbers from a text file named "data.txt" and store the average in a second file named "average.txt". to add a cell to a list, you append Think how to do that. finally you have to write the sum to a new file. Find. This is the assignment: Write a program that read numbers from a text file named "data.txt" and store the average in a second file named "average.txt". The numbers are line separated (each line in the file contains exactly one number.) You will have to create the file data.txt for testing purpose. I will post what I have thus far.

This script should accomplish what you are trying to do I think:

# define a list data structure to store the classgrades
classgrades = []

with open( 'classgrades.txt', 'r' ) as infile:
  for line in infile:
    l = line.split()
    # append a dict to the classgrades list with student as the key
    # and value is list of the students scores.
    classgrades.append({'name': l[0], 'scores': l[1:]})

with open( 'classscores.txt', 'w' ) as outfile:
  for student in classgrades:

    # get the students name out of dict.
    name = student['name']

    # get the students scores. use list comprehension to convert 
    # strings to ints so that scores is a list of ints.
    scores = [int(s) for s in student['scores']]

    # calc. total 
    total = sum(scores)

    # get the number of scores.
    count = len( student['scores'] )

    # calc. average
    average = total/count
    biggest = max(scores)
    smallest = min(scores)
    diff = ( biggest - smallest )

    outfile.write( "%s %s %s\n" % ( name, diff , average ))

Running the above code will create a file called classscores.txt which will contain this:

Chapman 45 79.33333333333333
Cleese 10 85.75
Gilliam 7 78.66666666666667
Idle 0 91.0
Jones 100 63.57142857142857
Palin 10 85.0

Calculating average and saving to a new file, I have to make a file with x and y as average of all ponts in interval of 10 (if there is no point in that interval it will give 0). i.e for 200 to 500 i will have 1 point for  Create files named input.txt and output.txt and put them in C:. Fill in some integers in the input.txt for example as shown below. 23 12 33 10 4 6 44 31 7 50 . Run the following program. /* Program to calculate the average of a list of numbers. */ /* calculate the total from one file, output the average */ /* into another file */ #include <stdio.h>

Bulletin of Prosthetics Research, A final check of residual limb dryness, water temperature, and zero-waterlevel output Measurements corresponding to different states of the residual limb (i.e., The average file then generates a normalization file by which all files are  How to calculate average of two columns and copy into another file? Hi, I need help with the awk command. I have a folder with aprox 500 files each one with two columns and I want to print in a new file, the average of column 1 and average of column 2 and the name of each file.

Occupational Outlook Handbook, For example, more and more traveling salespersons use portable, lap-top and utilities industries and manufacturing had higher average earnings than those in is another source of information about employment and training opportunities. They keep track of materials removed from the files and make sure that those  You then simply need to find the average of the rest of the entries in the array. You may need to use the Integer class' parseInt () method as these numbers will be encoded as strings.

Stochastic Networks, ( ρL1− ρ0 ) nL ifρi<1−ρ0,∀i = (1−ρ0)L−1 ∏ Ll=1(1 −ρ0 −ρl) , and thus π does indeed sum to Remark 8.7 We can compute the average time to transfer a file on route r but, as files on different routes may have very different sizes, a more useful  A Python program can read a text file using the built-in open() function. For example, below is a Python 3 program that opens lorem.txt for reading in text mode, reads the contents into a string variable named contents, closes the file, and then prints the data.