uaveiro-leci/1ano/fp/aula06/school.py

51 lines
1.2 KiB
Python
Executable File

# Complete o programa!
# a)
def loadFile(fname, lst):
with open(fname, 'r') as f:
for line in f:
line = line.strip('\n')
if not line[0].isnumeric():
continue
data = line.split('\t')
dataTuple = (int(data[0]), data[1], float(data[5]), float(data[6]), float(data[7]))
lst.append(dataTuple)
# b) Crie a função notaFinal aqui...
def notaFinal(reg):
return sum(reg[2:]) / 3
# c) Crie a função printPauta aqui...
# 4. Alterar a função...
def printPauta(lst, filename=""):
text = f'{"Numero":>6} {"Nome":^50} {"Nota":>4}\n'
for aluno in lst:
text += f'{aluno[0]:>6} {aluno[1]:^50} {notaFinal(aluno):>4.1f}\n'
if filename == "":
print(text)
else:
print(text, file=open(filename, 'w'))
# d)
def main():
lst = []
# ler os ficheiros (encontram-se na pasta 'datafiles' (~/1ano/fp/aula06/datafiles)
loadFile("datafiles/school1.csv", lst)
loadFile("datafiles/school2.csv", lst)
loadFile("datafiles/school3.csv", lst)
# ordenar a lista
lst.sort()
# mostrar a pauta
printPauta(lst, filename="datafiles/schooldata.txt")
# Call main function
if __name__ == "__main__":
main()