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

Asked

Viewed 906 times

0

I am learning some commands for analyzing data extracted from a CSV file and locked in the following situation:

I want to use as a basis this CSV to realize the average of 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 it, 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 result in the average of the room per test and after that the overall with all the evidence?

I just need it to be printed at the end:

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

With X being the average of all P1, Y the average of all P2, Z the average of all P3 and A the average of all banknotes.

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

  • Edit the question and detail the problem and the code to understand what the input and output data, so we can help you, we are not a forum for debates, there is no way to kick answer, give the minimum parameters. Thank you!

  • Edited publication.

  • Thanks Julia, and what is the expected "output" based on example CSV in your question?

  • Redacted.

  • Okay, put the results up as they should be, please.

1 answer

2


You started well but I have two comments on your code:

  1. It does not discard the header line present in CSV, so the content Nome;P1;P2;P3 would be interpreted as a student’s grades;

  2. When reading from the file, you will have the notes as string, but to do mathematical operations, you’ll 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])

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 number of records in turma:

quantidade_alunos = len(turma)

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

quantidade_provas = len(turma[0])

To calculate the average of each student, just 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, just add the grades 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 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, cannot be done:

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)

Thus, in the same way, notas will receive the list of notes.

  • I tried the same, but I’m getting the error "Syntaxerror: invalid syntax" in "name, *notes = line"

  • @Juliaricci Yeah, I ended up putting as Python 3. Do you really need to do in 2.7? Why?

  • Unfortunately it does, because it is the only one accepted in the programming of internal programs in my company. It would be possible to adapt to Python 2.7?

  • @Juliaricci I put at the end of the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.