How to perform calculations on top of a CSV file with Python 2.7?

0

I'm learning some commands for parsing data extracted from a CSV file and trapping in the following situation:

I want to use this CSV as the basis for averaging the room:

Nome;P1;P2;P3
Maria;8;7;10
Julia;9.5;10;7
Ailton;7;8;10
Leo;4;5;3

So far I have this, which I know to be the beginning:

import csv
with open('notas.csv', 'rb') as csvfile:
    csv = csv.reader(csvfile, delimiter=';', quotechar='|')
    lista = []
    for row in csv:
        lista.append(row)

How do I get the average room per test and after that the overall with all the tests?

I just need it printed at the end:

Média P1: X, média P2: Y, média P3: Z. Média final: A

With X the average of all P1, Y is the average of all P2, Z the average of all P3 and A average of all grades.

If more evidence or people are added, the account should continue to be made with the same code.

    
asked by anonymous 13.07.2018 / 19:47

1 answer

2

You started well, but I have two comments about your code:

  • It does not discard the header line present in the CSV, so the Nome;P1;P2;P3 content would be interpreted as a student's grade;

  • When reading from the file, you will have the notes as string , but to do math operations, you will need them as numbers.

    So by correcting these two items, it would look like:

    import csv
    
    turma = []
    
    with open('data.csv') as stream:
        reader = csv.reader(stream, delimiter=';')
        next(reader)  # Descarta o cabeçalho
        for line in reader:
            nome, *notas = line
            turma.append([float(nota) for nota in notas])
    

    By doing this, the object turma would be:

    [
        [8.0, 7.0, 10.0], 
        [9.5, 10.0, 7.0], 
        [7.0, 8.0, 10.0], 
        [4.0, 5.0, 3.0]
    ]
    

    The number of students will be the amount of records in turma :

    quantidade_alunos = len(turma)
    

    The amount of evidence will be the amount of grades each student has:

    quantidade_provas = len(turma[0])
    

    To calculate the average of each student, simply add your grades and divide by the number of tests:

    media_aluno_0 = sum(turma[0]) / quantidade_provas
    

    To calculate the class average in a test, simply add the scores and divide by the number of students:

    media_prova_1 = sum(provas[0] for provas in turma) / quantidade_alunos
    

    And finally, to calculate the class average, just add up all the grades and divide by the number of grades, which is the product of the number of students by the amount of tests:

    quantidade_notas = quantidade_alunos * quantidade_provas
    media_total = sum(sum(provas) for provas in turma) / quantidade_notas
    

    For Python version 2.7, you can not do:

    nome, *notas = line
    

    To get the same result, we can define an auxiliary function:

    def unpacking(a, *b): return a, b
    

    And use it instead:

    nome, notas = unpacking(*line)
    

    In this way, notas will receive the list of notes.

        
  • 13.07.2018 / 21:08