我以列表的形式存储记录。
编写一个菜单驱动程序,在二进制文件中追加、显示和删除学生详细信息 [id, name, percent]。删除应根据学生证进行。
所以我写了这个:
#Code to append display and delete student details
import pickle
def append():
f = open("student.bin","ab")
x = int(input("Enter number of students to be appended: "))
for i in range(x):
y = eval(input("Enter details in the form [id, name, percentage]: "))
pickle.dump(y,f)
f.close()
def display():
f = open("student.bin","rb")
while True:
try:
x = pickle.load(f)
print(x)
except EOFError:
break
f.close()
def delete():
f = open("student.bin","ab+")
x = input("Enter id to be deleted: ")
y = pickle.load(f)
for i in y:
if str(i[0]) == str(x):
最后,我将用户输入与匹配的 ID 匹配,但我不知道应该如何从二进制文件中完全删除列表。