read a txt file and list approved candidates

Asked

Viewed 231 times

0

Read a 'data.txt' file (attached) and list the approved candidates. The.txt data file is organized into six columns, separated by ";" and with the following content: column1>Cód. Mat, column2>full name, columns 3 to 6> notes in proofs P1 to P4, with decimal (separated by ","

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

The outgoing list must contain the name of the candidate (approved), with 60 positions and his final grade, with four positions (being one decimal)

NOTE: So far 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)

inserir a descrição da imagem aqui

  • Where is the sample of the mentioned input file ?

  • 0816187-9;ZULEIDE MARIA NAZARIO VERAS;1,5;2,25;0,75;8 0123456-0;JESUS OF 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;

  • Something like that is above

2 answers

0

Assuming that your input file (dados.txt) be 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.

Follows a code capable of determining students approved from a file .CSV:

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)

Exit:

[['JESUS DE NAZARE', 30.75], ['MICHAEL JACKSON', 25.75]]
  • Blz! That’s right, but the file is in .txt. Sorry for the lack of knowledge, is that I am new in this area of programming.

  • openArq = open(data.txt, 'r')

  • for line in openArq:

  • the exit with the print()

  • @Wendsonoliveira: ops, corrected!

-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 little about how it can be done, because your explanation about the notes and txt got a little confused, any mistake, just adapt with what I’ve already done.

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))
  • Blz! I will check. Very grateful!

  • To be clearer is thus example: 0816187-9;ZULEIDE MARIA NAZARIO VERAS;1,5;2,25;0,75;8;75

  • Matricula - Name - Nota1,nota2,nota3,nota4

  • List approved students. A candidate is approved with a higher average or equals to 25 and none of the grades can be less than 2.

  • The exit listing must contain the name of the candidate (approved), with 60 positions and his final (average) note, with four positions (being one decimal)

  • 1

    print("Student: %s nNote: %.2f n", line[0], float(somaNotas)) # Syntaxerror: invalid syntax

  • @Wendsonoliveira: Your example record has 7 fields while your question only mentions 6. Which would be correct ?

  • This code is not correct: There is a SyntaxError in the if due to lack of : at the end of the line; The output formatting in the print will not work as expected.

  • 6 fields: matricula, student, Nota1, nota2, nota3, nota4

  • As I said, I had not understood well what he had explained... I wrote more to try to help him find a way to do it. If he managed to finish the job, he did the job :)

Show 5 more comments

Browser other questions tagged

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