-
Notifications
You must be signed in to change notification settings - Fork 1
/
store_load_data.py
71 lines (55 loc) · 2.48 KB
/
store_load_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
This file is used to read and write data to pickle file
"""
__all__ = ["storeData", 'loadData']
import pickle
def storeData():
try:
with open("students_data.pkl", 'rb') as student_data:
students = pickle.load(student_data)
except FileNotFoundError:
# creating a new dict if file is doesn't exist
students = dict()
name = input("\nEnter Student's Name: ")
f_name = input("Enter Student's Father Name: ")
roll_no = input("Enter Student's Roll No: ")
maths = int(input("Enter Student's Mathematics marks: "))
english = int(input("Enter Student's English marks: "))
science = int(input("Enter Student's Science marks: "))
total = int(english) + int(maths) + int(science)
percentage = round(int(total) / 3, 2)
if roll_no in students:
print("Editing previous data")
students[roll_no] = [name, f_name, maths, english, science, total, percentage]
else:
students[roll_no] = [name, f_name, maths, english, science, total, percentage]
with open("students_data.pkl", 'wb') as student_data:
students = {key: value for key, value in
sorted(students.items(), key=lambda item: item[1][-1], reverse=True)}
pickle.dump(students, student_data)
def loadData(entity):
with open("students_data.pkl", 'rb') as student_data:
students = pickle.load(student_data)
students = list(students.items())
for i in range(len(students)):
if entity == students[i][0] or entity.upper() == students[i][1][0].upper():
print("\n")
print("=" * 50)
print("Ethans Public School".center(50))
print("=" * 50)
print("Student Name : ", students[i][1][0])
print("Father Name : ", students[i][1][1])
print("Roll Number : ", int(students[i][0]))
print('-' * 50)
print("English : ", students[i][1][2])
print("Maths : ", students[i][1][3])
print("Science : ", students[i][1][4])
print("-" * 50)
print("Total Marks : ", students[i][1][5])
print("Percentage : ", students[i][1][6])
print("Rank : ", i+1)
break
else:
print("Data not Found")
close = input("\nPress Q to exit.")
exit()