Exercise python read Dat file

Asked

Viewed 717 times

0

I’m stuck on an exercise that I can’t think of how to solve. The exercise consists of a dat file that contains a line for each student in a class of students. Each student’s name is at the beginning of each line and is followed by their grades and asks them to write a program that prints the names of students who have more than six grades.

notas_students.dat

jose 10 15 20 30 40
pedro 23 16 19 22
suzana 8 22 17 14 32 17 24 21 2 9 11 17
gisela 12 28 21 45 26 10
joao 14 32 25 16 89

What I could do

arquivo = open('arquivos/notas_estudante.dat','r')
linha = arquivo.readlines()


for x in linha:
    print(x)    

1 answer

0

You can use in each row the method split() to obtain separate grades and then check which students have more than six grades by getting the size of the list from index 1 to the end. See below how it would look:

with open("arquivos/notas_estudante.dat") as file:

    for line in file.readlines():
        data = line.split()

        if len(data[1:]) > 6:
            print(data[0])

Browser other questions tagged

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