简体   繁体   中英

how to calculate the average of numbers and sort them in csv file in python

In this project, you have to write a program that reads the grades of different people from a csv file and performs the following calculations on the grades and saves the resulting values in a file. In this project, you have to implement 5 different tasks. Do not change the names of the functions in any way and implement all the code you want to do in the same defs. 1- calculate the average of each person and save it along with the name of each person, the output order of the names must be exactly equal to the order of the input file. 2- Save the rates in ascending order along with the name of each person. 3- Save the top three GPAs with each person's name. 4- Save the three low GPAs without each person's name. 5- Calculate and store the average grades. I did the first task but have problem with others.

import csv
# For the average
from statistics import mean 

def calculate_averages(grades, mean):
    with open("address of input csv file") as infile:
        reader=csv.reader(infile)
        with open("address of output csv file" , "w", newline="") as outfile:
            writer = csv.writer(outfile)
            for row in reader:
                name = row[0]
                grade_mean = (float(grade) for grade in row [1:])
                writer.writerow([row[0], mean(grade_mean)])




def calculate_sorted_averages(input_file_name, output_file_name):
    


def calculate_three_best(input_file_name, output_file_name):
    


def calculate_three_worst(input_file_name, output_file_name):
        


def calculate_average_of_averages(input_file_name, output_file_name):

for example, the grades csv file contains these:

mandana,5,7,3,15
hamid,3,9,4,20,9,1,8,16,0,5,2,4,7,2,1
sina,19,10,19,6,8,14,3
sara,0,5,20,14
soheila,13,2,5,1,3,10,12,4,13,17,7,7
ali,1,9
sarvin,0,16,16,13,19,2,17,8

true answer is:

import csv

# # For the average
from statistics import mean 
def calculate_averages(grades, mean):
    with open("address inputfile") as infile:
        reader=csv.reader(infile)
        with open("adress of outputfile" , "w", newline="") as outfile:
            writer = csv.writer(outfile)
            for row in reader:
                name = row[0]
                grade_mean = (float(grade) for grade in row [1:])
                writer.writerow([row[0], mean(grade_mean)])




def calculate_sorted_averages(input_file_name, output_file_name):
    avarage_list = []
    with open(input_file_name) as infile:
        reader=csv.reader(infile)
        with open(output_file_name , "w", newline="") as outfile:
            writer = csv.writer(outfile)
            for row in reader:
                name = row[0]
                grade_mean = (float(grade) for grade in row [1:])
                avarage_list.append([name, mean(grade_mean)])
            avarage_list.sort(key = lambda x: x[1])
            for item in avarage_list:
                writer.writerow([item[0], float(item[1])])


def calculate_three_best(input_file_name, output_file_name):
    avarage_list = []
    with open(input_file_name) as infile:
        reader=csv.reader(infile)
        with open(output_file_name , "w", newline="") as outfile:
            writer = csv.writer(outfile)
            for row in reader:
                name = row[0]
                grade_mean = (float(grade) for grade in row [1:])
                avarage_list.append([name, mean(grade_mean)])
            avarage_list.sort(key = lambda x: x[1], reverse=True)
            for item in avarage_list[:3]:
                writer.writerow([item[0], float(item[1])])


def calculate_three_worst(input_file_name, output_file_name):
    avarage_list = []
    with open(input_file_name) as infile:
        reader=csv.reader(infile)
        with open(output_file_name , "w", newline="") as outfile:
            writer = csv.writer(outfile)
            for row in reader:
                name = row[0]
                grade_mean = (float(grade) for grade in row [1:])
                avarage_list.append([name, mean(grade_mean)])
            avarage_list = sorted(sorted(avarage_list, key = lambda x : x[0]), key = lambda x : x[1], reverse = True)
            avarage_list_worst = avarage_list[-3:]
            avarage_list_worst.sort(key = lambda x: x[1])
            for item in avarage_list_worst:
                writer.writerow([float(item[1])])          


def calculate_average_of_averages(input_file_name, output_file_name):
    avarage_list = []
    with open(input_file_name) as infile:
        reader=csv.reader(infile)
        with open(output_file_name , "w", newline="") as outfile:
            writer = csv.writer(outfile)
            for row in reader:
                name = row[0]
                grade_mean = (float(grade) for grade in row [1:])
                avarage_list.append([name, mean(grade_mean)])
            all_mean = [float(el[1]) for el in avarage_list]
            writer.writerow([mean(all_mean)])

            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM