如何使用 Python 删除存储在 Pickle 文件中的对象
How to remove an object stored in a Pickle file with Python
我正在开发一个将对象存储在 pickle 文件中的数据库程序。这些对象具有三个属性:名称、成绩和平均水平。我已经能够将每个 Student 对象保存到一个 pickle 文件中,并且我可以读回该文件并显示它的内容。但是,我不知道如何使用索引号从文件中删除对象。我希望用户能够输入一个数字,然后删除那个带编号的对象(所以如果我输入 2,第二个对象就会被删除。)现在我的代码能够从 pickle 列表中删除一个项目,但是我无法让它与一个对象一起工作。当我尝试删除学生时,出现以下错误:TypeError: 'Student' object does not support item deletion。有谁知道如何从我的 pickle 文件中删除对象?
我的代码如下:
import pickle
class Student():
def __init__(self,nam,grd,avg):
self.name = nam
self.grade = grd
self.average = avg
def get_details(self):
print(self.name, self.grade, self.average)
def create_item():
new_student = Student(input("Enter name: "),input("Enter grade: "), input("Enter average: "))
save_object(new_student, 'student_data.pkl')
def clear_database():
file = open('student_data.pkl', 'w')
file.close()
def save_object(obj, filename):
with open(filename, 'ab') as output:
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
def unpickle_database(filename):
with open(filename, 'rb') as f:
while True:
try:
yield pickle.load(f)
except EOFError:
break
def display_database():
print("\nName: Grade: Average: ")
for student in unpickle_database('student_data.pkl'):
property_list = [student.name, student.grade, student.average]
print(''.join([v.ljust(20,' ') for v in property_list]))
def delete_student(student_to_delete):
with open('student_data.pkl', 'rb') as f:
db = pickle.load(f)
try:
del db[student_to_delete]
except KeyError:
print("{user} doesn't exist in db".format(user=user_to_delete))
while True:
user_input = input("\nType \"Clear\" to clear the database. Type \"Add\" to add a student. Type \"Display\" to display the database contents. Type \"Quit\" to quit the program. Type \"Remove\" to remove a student.\n")
if user_input == "Quit":
break
if user_input == "Clear":
Student.clear_database()
print("\nThe database has been cleared.")
elif user_input == "Add":
Student.create_item()
display_database()
elif user_input == "Display":
display_database()
elif user_input == "Remove":
student_to_delete = input("Type the student number that you would like to delete: ")
delete_student(student_to_delete)
您不能直接从 Pickle 文件中删除对象。我的意思是,你可以这样做,但它需要你解析它的结构以确定单个对象的数据从哪里开始和结束。这很乏味,而且不是很有收获。
不过,您可以做的是将所有腌制学生作为列表阅读:
students = list(unpickle_database(filename))
然后,使用 del students[index]
从列表 中删除一名学生 ,最后使用上述列表重新挑选所有剩余学生:
for student in students:
save_object(student, filename)
我正在开发一个将对象存储在 pickle 文件中的数据库程序。这些对象具有三个属性:名称、成绩和平均水平。我已经能够将每个 Student 对象保存到一个 pickle 文件中,并且我可以读回该文件并显示它的内容。但是,我不知道如何使用索引号从文件中删除对象。我希望用户能够输入一个数字,然后删除那个带编号的对象(所以如果我输入 2,第二个对象就会被删除。)现在我的代码能够从 pickle 列表中删除一个项目,但是我无法让它与一个对象一起工作。当我尝试删除学生时,出现以下错误:TypeError: 'Student' object does not support item deletion。有谁知道如何从我的 pickle 文件中删除对象?
我的代码如下:
import pickle
class Student():
def __init__(self,nam,grd,avg):
self.name = nam
self.grade = grd
self.average = avg
def get_details(self):
print(self.name, self.grade, self.average)
def create_item():
new_student = Student(input("Enter name: "),input("Enter grade: "), input("Enter average: "))
save_object(new_student, 'student_data.pkl')
def clear_database():
file = open('student_data.pkl', 'w')
file.close()
def save_object(obj, filename):
with open(filename, 'ab') as output:
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
def unpickle_database(filename):
with open(filename, 'rb') as f:
while True:
try:
yield pickle.load(f)
except EOFError:
break
def display_database():
print("\nName: Grade: Average: ")
for student in unpickle_database('student_data.pkl'):
property_list = [student.name, student.grade, student.average]
print(''.join([v.ljust(20,' ') for v in property_list]))
def delete_student(student_to_delete):
with open('student_data.pkl', 'rb') as f:
db = pickle.load(f)
try:
del db[student_to_delete]
except KeyError:
print("{user} doesn't exist in db".format(user=user_to_delete))
while True:
user_input = input("\nType \"Clear\" to clear the database. Type \"Add\" to add a student. Type \"Display\" to display the database contents. Type \"Quit\" to quit the program. Type \"Remove\" to remove a student.\n")
if user_input == "Quit":
break
if user_input == "Clear":
Student.clear_database()
print("\nThe database has been cleared.")
elif user_input == "Add":
Student.create_item()
display_database()
elif user_input == "Display":
display_database()
elif user_input == "Remove":
student_to_delete = input("Type the student number that you would like to delete: ")
delete_student(student_to_delete)
您不能直接从 Pickle 文件中删除对象。我的意思是,你可以这样做,但它需要你解析它的结构以确定单个对象的数据从哪里开始和结束。这很乏味,而且不是很有收获。
不过,您可以做的是将所有腌制学生作为列表阅读:
students = list(unpickle_database(filename))
然后,使用 del students[index]
从列表 中删除一名学生 ,最后使用上述列表重新挑选所有剩余学生:
for student in students:
save_object(student, filename)