I have a text file containing:
Number Name subject1 subject2 subject3 subject4 subject5
1234567 Jan 5 7 0 6 4
3526435 Marie 5 5 7 0 0
2230431 Kees 6 10 0 8 6
7685433 André 4 7 8 7 5
0364678 Antoinette 0 2 8 8 8
1424354 Jerôme 7 9 0 5 0
4536576 Kamal 8 0 8 7 8
1256033 Diana 0 0 0 0 0
5504657 Petra 6 6 7 0 6
9676575 Malika 0 6 0 0 8
0253756 Samira 3 8 6 7 10
I want to calculate the average grades for each student (if grade = 0 then it is not included), and I want to calculate the average grades for each subject (again, without calculating 0).
In my own code I copied all of the information and put it into lists.
The problem I am facing, is that I need my Python program to read the text file and calculate with the given numbers.
So far, this is all I have:
i = 0
file = open("resultaten.txt", "r")
for x in file:
if i == 0:
print("Lines: ")
else:
x = x.split()
print(i, x)
i +=1
How would one use a text file to calculate specific characters in a line?
Thanks in advance.
If we convert this to a dictionary we will have a lot of flexibility with what we want to do with the information. This can be done with a little effort. We can use the first line to create our keys
then we can zip those keys with each other line, then create a list of tuples by zipping those lists. From there we can use a dictionary constructor to create our list of dictionaries. Now we just have to gather all keys
from this list of dictionaries that are subjects
for each item in the list, map those to ints and create an exception for when the student has scored all 0
s. If not we filter out the 0
's from a complete list and then calculate average. Next to get averages for each subject
we can extract all the values that are connected to that subject, not taking values that are 0
, same we map ints
and then calculate average. I threw in some text justification for appearances, not neccessary. The process for the remaining subjects would be the same just swap out the subject.
with open('text.txt') as f:
content = [line.split() for line in f]
keys = content[0]
lst = list(zip([keys]*(len(content)-1), content[1:]))
x = [zip(i[0], i[1]) for i in lst]
z = [dict(i) for i in x]
print('Average Grades'.center(30))
for i in z:
subs =[i['subject1'], i['subject2'], i['subject3'], i['subject4'], i['subject5']]
subs = list(map(int, subs))
if sum(subs) == 0:
print('{:<10} average grade: {:>4}'.format(i['Name'], 0))
else:
subs = list(filter(lambda x: x >0, subs))
avg = round(sum(subs)/len(subs), 2)
print('{:<10} average grade: {:>4}'.format(i['Name'], avg))
sub1 = [i['subject1'] for i in z if i['subject1'] != '0']
sub1 = list(map(int, sub1))
sub1_avg = sum(sub1)/len(sub1)
print('\nAverage Grade for Subject 1: {}'.format(sub1_avg))
Average Grades Jan average grade: 5.5 Marie average grade: 5.67 Kees average grade: 7.5 André average grade: 6.2 Antoinette average grade: 6.5 Jerôme average grade: 7.0 Kamal average grade: 7.75 Diana average grade: 0 Petra average grade: 6.25 Malika average grade: 7.0 Samira average grade: 6.8 Average Grade for Subject 1: 5.5