python将学生信息保存到文件中_Python学生信息管理系统(菜单界面,TXT保存文件),txt...
时间: 2024-03-14 07:48:02 浏览: 57
学生信息管理系统(Python)
好的,我可以为您提供代码实现。以下是一个简单的Python学生信息管理系统,可以将学生信息保存到txt文件中。请注意,此代码仅供参考,您可以根据自己的需求进行修改和优化。
```python
import os
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def __str__(self):
return f"Name: {self.name}\nAge: {self.age}\nGender: {self.gender}\n"
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
return True
return False
def get_all_students(self):
return self.students
def save_to_file(self, file_name):
with open(file_name, "w") as f:
for student in self.students:
f.write(f"{student.name},{student.age},{student.gender}\n")
def load_from_file(self, file_name):
if not os.path.exists(file_name):
return
with open(file_name, "r") as f:
lines = f.readlines()
for line in lines:
fields = line.strip().split(",")
name = fields[0]
age = int(fields[1])
gender = fields[2]
student = Student(name, age, gender)
self.add_student(student)
def display_menu():
print("1. Add student")
print("2. Remove student")
print("3. View all students")
print("4. Save to file")
print("5. Load from file")
print("6. Quit")
def get_menu_choice():
choice = input("Enter your choice: ")
while choice not in ["1", "2", "3", "4", "5", "6"]:
choice = input("Invalid choice. Enter your choice again: ")
return choice
def add_student(manager):
name = input("Enter the student's name: ")
age = int(input("Enter the student's age: "))
gender = input("Enter the student's gender: ")
student = Student(name, age, gender)
manager.add_student(student)
print("Student added successfully.")
def remove_student(manager):
name = input("Enter the name of the student to be removed: ")
if manager.remove_student(name):
print("Student removed successfully.")
else:
print("Student not found.")
def view_all_students(manager):
students = manager.get_all_students()
if not students:
print("No students found.")
else:
for student in students:
print(student)
def save_to_file(manager):
file_name = input("Enter the file name: ")
manager.save_to_file(file_name)
print("Data saved to file successfully.")
def load_from_file(manager):
file_name = input("Enter the file name: ")
manager.load_from_file(file_name)
print("Data loaded from file successfully.")
def main():
manager = StudentManager()
while True:
display_menu()
choice = get_menu_choice()
if choice == "1":
add_student(manager)
elif choice == "2":
remove_student(manager)
elif choice == "3":
view_all_students(manager)
elif choice == "4":
save_to_file(manager)
elif choice == "5":
load_from_file(manager)
elif choice == "6":
break
if __name__ == "__main__":
main()
```
此代码使用了面向对象的编程方式,定义了一个Student类和一个StudentManager类,用于管理学生信息。其中,StudentManager类实现了添加、删除、查看和保存学生信息的功能,并且可以将学生信息保存到txt文件中,也可以从txt文件中读取学生信息。在main函数中,通过一个无限循环来不断显示菜单,并根据用户的选择来执行相应的操作。
阅读全文