编写一个python学生信息管理系统实现显示所有学生信息,新建学生信息,删除学生信息,查询学生信息,修改学生信息,保存学生信息,恢复学生信息等功能的代码
时间: 2024-03-27 10:36:47 浏览: 29
好的,以下是一个基于Python的学生信息管理系统的代码,其中实现了显示所有学生信息,新建学生信息,删除学生信息,查询学生信息,修改学生信息,保存学生信息,恢复学生信息等功能:
```python
import csv
# 定义一个学生信息类
class Student:
def __init__(self, name, age, gender, id):
self.name = name
self.age = age
self.gender = gender
self.id = id
def __repr__(self):
return f'Name: {self.name}, Age: {self.age}, Gender: {self.gender}, ID: {self.id}'
# 定义一个学生信息管理系统类
class StudentManagementSystem:
def __init__(self, file_path):
self.file_path = file_path
self.students = []
# 显示所有学生信息
def show_all_students(self):
if len(self.students) == 0:
print("No student information found.")
else:
for student in self.students:
print(student)
# 新建学生信息
def add_student(self, name, age, gender, id):
student = Student(name, age, gender, id)
self.students.append(student)
print("New student information added.")
# 删除学生信息
def delete_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
print("Student information deleted.")
break
else:
print("Student information not found.")
# 查询学生信息
def search_student(self, id):
for student in self.students:
if student.id == id:
print(student)
break
else:
print("Student information not found.")
# 修改学生信息
def modify_student(self, id, name=None, age=None, gender=None):
for student in self.students:
if student.id == id:
if name:
student.name = name
if age:
student.age = age
if gender:
student.gender = gender
print("Student information modified.")
break
else:
print("Student information not found.")
# 保存学生信息
def save_students(self):
with open(self.file_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Name', 'Age', 'Gender', 'ID'])
for student in self.students:
writer.writerow([student.name, student.age, student.gender, student.id])
print("Student information saved.")
# 恢复学生信息
def load_students(self):
try:
with open(self.file_path, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
student = Student(row['Name'], row['Age'], row['Gender'], row['ID'])
self.students.append(student)
print("Student information loaded.")
except FileNotFoundError:
print("Student information file not found.")
# 测试代码
sms = StudentManagementSystem('students.csv')
sms.load_students()
sms.show_all_students()
sms.add_student('Alice', 18, 'Female', '001')
sms.add_student('Bob', 19, 'Male', '002')
sms.show_all_students()
sms.modify_student('001', name='Alex')
sms.show_all_students()
sms.search_student('002')
sms.delete_student('001')
sms.save_students()
```
这个代码实现了一个简单的学生信息管理系统,并通过CSV文件存储了学生信息。你可以根据自己的需求对这个代码进行修改与扩充。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)