read a txt file and list the successful candidates

0

Read a 'data.txt' file (attached) and list approved candidates. The data.txt file is organized into six columns, separated by ";" , and with the following content: column1> code. Mat, column 2 > full name, columns 3 to 6 > notes in tests p1 to p4, with decimal (separated by ","

A candidate is approved when the sum of their marks (the final grade) is greater than or equal to 25 and none of them is less than 2.

The exit list should contain the name of the candidate (approved), with 60 positions and its final mark, with four positions (one decimal place)

NOTE: Until now I just opened and read the file

nomeArq = input('nome do arquivo: ')
Lista = opone(nomeArq, 'r')

for linha in Lista: #Lê linha por linha
   linha = linha.rstrip()#rstrip remove os espaços em branco entre linhas
   print(linha)

    
asked by anonymous 10.06.2018 / 02:19

2 answers

0

Assuming your input file ( dados.txt ) is something like:

0816187-9;ZULEIDE MARIA NAZARIO VERAS;1,5;2,25;0,75;8
0123456-0;JESUS DE NAZARE;8,5;9,25;3,75;9,25
9876543-9;CHAPOLIN COLORADO;1,5;5,25;0,75;8,75
2468246-1;MICHAEL JACKSON;4,5;8,5;7,5;5,25
0101010-1;HOMER SIMPSON;1,5;2,25;0,75;6,25

According to the rules, only JESUS DE NAZARE and MICHAEL JACKSON would be approved.

Here is a code that can determine approved students from a .CSV file:

import csv

arquivo = "dados.txt"
saida = []

with open( arquivo ) as arqcsv:
    leitor = csv.reader( arqcsv, delimiter=';')
    for linha in leitor:
        notas = [ float(nota.replace(',', '.')) for nota in linha[2:6] ]
        if( all( nota >= 2.0 for nota in notas ) ):
            if( sum(notas) >= 25.0 ):
                saida.append([linha[1], sum(notas)])
print(saida)

Output:

[['JESUS DE NAZARE', 30.75], ['MICHAEL JACKSON', 25.75]]
    
10.06.2018 / 13:27
-1

From what I understood from the txt document, I opened the file here, separated them by ";" and left everyone ready in a list. See if my attempt clarifies a bit about how it can be done, because your explanation of the notes and the txt was a bit confusing, any error, just adapt with what I already did.

nomeArq = input('nome do arquivo: ') 

with open(nomeArq, 'r') as arq:
    Lista = []
    #Lê linha por linha 
    for linha in arq:
        # Notas na coluna 3, 4, 5 e 6. Separei cada coluna aqui.
        linha.split(";")
        # Calcula a media aqui
        somaNotas = float(linha[2]) + float(linha[3]) + float(linha[4]) + float(linha[5])
        if(somaNotas >= 25 and (float(linha[2]) or float(linha[3]) or float(linha[4]) or float(linha[5]) < 2))
            print("Aluno: %s\nNota: %.2f\n", linha[0], float(somaNotas))
    
10.06.2018 / 03:00