学生管理系统代码,可以在原有的文件上修改删除学生
时间: 2024-04-16 21:28:04 浏览: 14
以下是一个简单的示例代码,展示了如何在原有文件上进行学生信息的修改和删除操作:
```python
import csv
def read_students(file_path):
students = []
with open(file_path, 'r') as file:
reader = csv.reader(file)
for row in reader:
students.append(row)
return students
def write_students(file_path, students):
with open(file_path, 'w', newline='') as file:
writer = csv.writer(file)
for student in students:
writer.writerow(student)
def modify_student(students, student_id, new_data):
for i in range(len(students)):
if students[i][0] == student_id:
students[i] = new_data
break
def delete_student(students, student_id):
for i in range(len(students)):
if students[i][0] == student_id:
del students[i]
break
# 示例使用
file_path = 'students.csv'
students = read_students(file_path)
# 修改学生信息
student_id = '12345'
new_data = ['12345', 'Alice', 'Grade 10']
modify_student(students, student_id, new_data)
# 删除学生信息
student_id = '98765'
delete_student(students, student_id)
# 将修改后的学生信息写回文件
write_students(file_path, students)
```
请注意,这只是一个简单的示例代码,实际开发中可能需要根据具体的需求进行更多的错误处理、数据验证等操作。另外,该示例使用了 CSV 文件作为数据存储的格式,您也可以根据需要选择其他合适的文件格式或数据库来存储学生信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)