删除python列表中输入的行信息
时间: 2024-04-08 16:33:39 浏览: 46
python删除列表对象
好的,以下是一个简单的学生信息管理系统的Python实现:
```python
# 定义一个学生类
class Student:
def __init__(self, name, age, gender, id):
self.name = name
self.age = age
self.gender = gender
self.id = id
# 定义一个学生信息管理系统类
class StudentManagementSystem:
def __init__(self):
self.students = []
# 添加学生信息
def add_student(self, student):
self.students.append(student)
# 显示所有学生信息
def show_all_students(self):
for student in self.students:
print(f"姓名:{student.name},年龄:{student.age},性别:{student.gender},学号:{student.id}")
# 根据学号查询学生信息
def search_student_by_id(self, id):
for student in self.students:
if student.id == id:
print(f"姓名:{student.name},年龄:{student.age},性别:{student.gender},学号:{student.id}")
return
print("未找到该学生")
# 根据学号修改学生信息
def modify_student_by_id(self, id, name, age, gender):
for student in self.students:
if student.id == id:
student.name = name
student.age = age
student.gender = gender
print("修改成功")
return
print("未找到该学生")
# 根据学号删除学生信息
def delete_student_by_id(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
print("删除成功")
return
print("未找到该学生")
# 保存学生信息到文件
def save_students_to_file(self, filename):
with open(filename, "w") as f:
for student in self.students:
f.write(f"{student.name},{student.age},{student.gender},{student.id}\n")
# 从文件中读取学生信息
def load_students_from_file(self, filename):
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
fields = line.strip().split(",")
name = fields[]
age = int(fields[1])
gender = fields[2]
id = fields[3]
student = Student(name, age, gender, id)
self.add_student(student)
# 测试代码
if __name__ == "__main__":
sms = StudentManagementSystem()
sms.add_student(Student("张三", 18, "男", "001"))
sms.add_student(Student("李四", 19, "女", "002"))
sms.add_student(Student("王五", 20, "男", "003"))
sms.show_all_students()
sms.search_student_by_id("002")
sms.modify_student_by_id("002", "小李", 20, "女")
sms.show_all_students()
sms.delete_student_by_id("003")
sms.show_all_students()
sms.save_students_to_file("students.txt")
sms.load_students_from_file("students.txt")
sms.show_all_students()
```
这个学生信息管理系统包含了添加学生信息、显示所有学生信息、根据学号查询学生信息、根据学号修改学生信息、根据学号删除学生信息、保存学生信息到文件和从文件中读取学生信息等功能。你可以根据自己的需求进行修改和扩展。
阅读全文