基于Python的学生信息管理系统 v2.0
一、前言
1.1 思路
实现增删改查的功能模块,本人开发了基于python的学生信息管理系统的第一版本,第一版本有许多不足的地方,在此基础上进行改进。
第1版本学生信息管理系统:基于Python的学生成绩管理信息系统 v1.0-CSDN博客
1.2 改进
1.实现学生总成绩排序(排序从大到小顺序)
2.实现添加学生信息时候,不能出现重复学号的学生
3.添加学生信息时,后台最好有一个支撑的学生信息的数据,比如txt文件。(省的重新输入学生信息)增删改
-
增:利用添加在文件里末尾添加一行的信息即可
-
删:思路应该是有两种方法,我偏向于第二个
(1) 清理所有信息后,把对象的变量遍历填入到txt,基于对象遍历比较多
(2) 选中工号与txt核对遍历之后,清理。基于文件读写删比较多
二、改进前的准备材料
利用txt作为数据存储,目的来支撑学生管理信息系统的数据传输。需要学习Python文件读写具备小知识。
有个博客值得一看,值得参考~
【Python 文件读写】— txt文件_python读取txt文件-CSDN博客
尤其是读写模式和函数
r:以读方式打开文件,可读取文件信息。
w:以写方式打开文件,可向文件写入信息。如文件存在,则清空该文件,再写入新内容
a:以追加模式打开文件(即一打开文件,文件指针自动移到文件末尾),如果文件不存在则创建
r+:以读写方式打开文件,可对文件进行读和写操作。
w+:消除文件内容,然后以读写方式打开文件。
a+:以读写方式打开文件,并把文件指针移到文件尾。
read() : 读取文件全部内容,以字符串形式返回结果。
readline(): 读取文件第一行的内容,以字符串形式返回结果。
readlines(): 读取文件全部内容,以列表形式返回结果(可以指定行输出)。
三、基于txt文件读写方式实现学生信息管理系统(初步测试)
3.1 增加学生信息
def addStudent(name, age, grade):
try:
with open("students.txt", "a", encoding="utf-8") as f:
f.write(f"{name},{age},{grade}\n")
print("学生信息添加成功")
except IOError:
print("文件操作出错")
从上面代码可看出来,
a:以追加模式打开文件(即一打开文件,文件指针自动移到文件末尾),如果文件不存在则创建
with open("students.txt", "a", encoding="utf-8") as f:
f.write(f"{name},{age},{grade}\n")
3.2 删除学生信息
def deleteStudent(name):
try:
with open("students.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
with open("students.txt", "w", encoding="utf-8") as f:
for line in lines:
if name not in line:
f.write(line)
print("学生信息删除成功")
except IOError:
print("文件操作出错")
从上面看出,r是读取数据,w是写入数据。
尤其是w,以写方式打开文件,可向文件写入信息。如文件存在,则清空该文件,再写入新内容
3.3 修改学生信息
def updateStudent(name, new_age, new_grade):
try:
with open("students.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
with open("students.txt", "w", encoding="utf-8") as f:
for line in lines:
if name in line:
student_info = line.split(",")
student_info[1] = new_age
student_info[2] = new_grade
f.write(",".join(student_info))
else:
f.write(line)
print("学生信息更新成功")
except IOError:
print("文件操作出错")
通过函数遍历txt文件内容方式核对信息则更新。
四、 改进基于Python的学生信息管理系统v1.0
4.1.载入学生信息
如果有student.txt文件并且录入学生信息(工号,姓名,5个成绩),载入文件会显示有学生信息的数据。反之,显示没有任何学生信息的数据,需要你选项2添加学生信息才可以显示。
# 载入学生信息
def loadStudentInfo(self):
try:
with open("students.txt", "r") as f:
for line in f:
fields = line.strip().split(",")
# print(fields)
student_txt = Student(fields[0], fields[1], int(fields[2]), int(fields[3]), int(fields[4]), int(fields[5]), int(fields[6]))
self.students.append(student_txt)
print("\n信息: 载入学生信息成功!")
except FileNotFoundError:
print("指定的文件不存在,请确认文件名和路径是否正确")
except IOError:
print("无法打开文件,请检查文件权限和路径")
运行结果:
打开你的students.txt文件,和命令控制台cmd结果是一样的。
4.2 添加学生信息
代码改进地方是,不能重复工号
# 添加学生信息时,检查是否有重复工号的学生信息查找核对
def duplicateStudentIdInfo(self, add_id):
for student in self.students:
if add_id == student.id:
return True
else:
return False
在文件末尾添加一行信息。
# a+是追加模式,打开文件,文件指针自动移到文件末尾,如果文件不存在则创建。
try:
with open('students.txt', "a+") as f:
f.seek(0)
first_line = f.readline().strip()
if not first_line:
f.write("{},{},{},{},{},{},{}\n".format(str(add_id), str(add_name), str(add_chinese), str(add_math), str(add_english), str(add_physics), str(add_history)))
else:
f.write("\n{},{},{},{},{},{},{}".format(str(add_id), str(add_name), str(add_chinese), str(add_math), str(add_english), str(add_physics), str(add_history)))
print("信息: 添加成功!")
except IOError:
print("信息: 文件操作出错!")
以上代码,是实现添加在txt文件里,某种关键的代码。以下是增加学生信息函数的完整代码。
【综合】增加学生函数
# 添加学生信息函数
def addStudentInfo(self):
while True:
id = str(input("学号:"))
if not re.match(r'[0-9]{8}$', id):
print("错误: 学号必须是8位数!")
elif self.duplicateStudentIdInfo(id) == True:
print("提示:学号已存在!")
else:
add_id = str(id)
break
while True:
name = str(input("姓名:"))
if name == "":
print("提示:没填写名字!")
else:
add_name = name
break
while True:
chinese = input("语文成绩:")
if not re.match(r'^\d+$', chinese) or int(chinese) < 0 or int(chinese) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
add_chinese = int(chinese)
break
while True:
math = input("数学成绩:")
if not re.match(r'^\d+$', math) or int(math) < 0 or int(math) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
add_math = int(math)
break
while True:
english = input("英语成绩:")
if not re.match(r'^\d+$', english) or int(english) < 0 or int(english) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
add_english = int(english)
break
while True:
physics = input("物理成绩:")
if not re.match(r'^\d+$', physics) or int(physics) < 0 or int(physics) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
add_physics = int(physics)
break
while True:
history = input("历史成绩:")
if not re.match(r'^\d+$', history) or int(history) < 0 or int(history) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
add_history = int(history)
break
studentinfo = Student(add_id, add_name, add_chinese, add_math, add_english, add_physics, add_history)
self.students.append(studentinfo)
# a+是追加模式,打开文件,文件指针自动移到文件末尾,如果文件不存在则创建。
try:
with open('students.txt', "a+") as f:
f.seek(0)
first_line = f.readline().strip()
if not first_line:
f.write("{},{},{},{},{},{},{}\n".format(str(add_id), str(add_name), str(add_chinese), str(add_math), str(add_english), str(add_physics), str(add_history)))
else:
f.write("\n{},{},{},{},{},{},{}".format(str(add_id), str(add_name), str(add_chinese), str(add_math), str(add_english), str(add_physics), str(add_history)))
print("信息: 添加成功!")
except IOError:
print("信息: 文件操作出错!")
运行结果:
4.3 修改学生信息
和增加学生信息的函数原理是接近的,在此基础上稍微改进即可。
# 更新学生信息
try:
with open("students.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
print(lines)
with open("students.txt", "w", encoding="utf-8") as f:
for line in lines:
if revise_id in line:
student_info = line.split(",")
print(student_info)
student_info[1] = str(student.name)
student_info[2] = str(student.chinese)
student_info[3] = str(student.math)
student_info[4] = str(student.english)
student_info[5] = str(student.physics)
student_info[6] = str(student.history) + "\n"
print(student_info)
f.write(",".join(student_info))
else:
f.write(line)
print("成功: 学生信息已更新!")
except IOError:
print("文件操作出错")
完整的更新学生信息函数的代码
# 修改学生信息
def reviseStudentInfo(self):
self.showAllStudentInfo()
revise_id = input("查询学号:")
for student in self.students:
if revise_id == student.id:
print("当前学生信息为:")
print("-" * 92)
print("|{:^10s}|{:^10s}|{:^10s}|{:^10s}|{:^10s}|{:^10s}|{:^10s}|".format("学号", "姓名", "语文", "数学", "英语", "物理", "历史"))
print("-" * 92)
print("|{:^12s}|{:^12s}|{:^12d}|{:^12d}|{:^12d}|{:^12d}|{:^12d}|".format(student.id, student.name, student.chinese, student.math,student.english, student.physics, student.history))
print("-" * 92)
print("修改当前学生信息:")
while True:
name = str(input("请输入新的姓名(留空表示不修改):"))
if name == "":
break
else:
student.name = name
break
while True:
chinese = input("请输入新的语文成绩(留空表示不修改):")
if chinese == "":
break
elif not re.match(r'^\d+$', chinese) or int(chinese) < 0 or int(chinese) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
student.chinese = int(chinese)
break
while True:
math = input("请输入新的数学成绩(留空表示不修改):")
if math == "":
break
elif not re.match(r'^\d+$', math) or int(math) < 0 or int(math) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
student.math = int(math)
break
while True:
english = input("请输入新的英语成绩(留空表示不修改):")
if english == "":
break
elif not re.match(r'^\d+$', english) or int(english) < 0 or int(english) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
student.english = int(english)
break
while True:
physics = input("请输入新的物理成绩(留空表示不修改):")
if physics == "":
break
elif not re.match(r'^\d+$', physics) or int(physics) < 0 or int(physics) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
student.physics = int(physics)
break
while True:
history = input("请输入新的历史成绩(留空表示不修改):")
if history == "":
break
elif not re.match(r'^\d+$', history) or int(history) < 0 or int(history) > 100:
print("错误: 成绩必须为0-100之间的整数!")
else:
student.history = int(history)
break
# 更新学生信息
try:
with open("students.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
print(lines)
with open("students.txt", "w", encoding="utf-8") as f:
for line in lines:
if revise_id in line:
student_info = line.split(",")
print(student_info)
student_info[1] = str(student.name)
student_info[2] = str(student.chinese)
student_info[3] = str(student.math)
student_info[4] = str(student.english)
student_info[5] = str(student.physics)
student_info[6] = str(student.history) + "\n"
print(student_info)
f.write(",".join(student_info))
else:
f.write(line)
print("成功: 学生信息已更新!")
except IOError:
print("文件操作出错")
return
print("信息:未找到匹配的学生信息,无法修改!")
运行结果:
我想要给keli学生,更改一下信息。
4.4 删除学生信息
# 删除学生信息
def deleteStudentInfo(self):
self.showAllStudentInfo()
delete_id = input("删除学号:")
for student in self.students:
if delete_id == student.id:
self.students.remove(student)
try:
with open("students.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
with open("students.txt", "w", encoding="utf-8") as f:
for line in lines:
if delete_id not in line:
f.write(line)
print("成功删除学号为{}的学生信息!".format(delete_id))
return
except IOError:
print("文件操作出错")
print("信息:未找到匹配的学生信息,无法删除!")
return None
运行结果:
4.5 排序总成绩的学生
利用冒泡排序算法
# 排序所有学生总成绩信息(从大到小)
def sortStudentScoreInfo(self):
print("-" * 106)
print("|{:^10s}|{:^10s}|{:^10s}|{:^10s}|{:^10s}|{:^10s}|{:^10s}|{:^10s}|".format("学号", "姓名", "语文", "数学", "英语", "物理", "历史", "总成绩"))
print("-" * 106)
# 冒泡排序
n = len(self.students)
for i in range(n-1):
for j in range(n-1-i):
total_score1 = self.students[j].chinese + self.students[j].math + self.students[j].english + self.students[j].physics + self.students[j].history
total_score2 = self.students[j+1].chinese + self.students[j+1].math + self.students[j+1].english + self.students[j+1].physics + self.students[j+1].history
# 进行交换
if total_score1 < total_score2:
self.students[j], self.students[j+1] = self.students[j+1], self.students[j]
for student in self.students:
total_score = student.chinese + student.math + student.english + student.physics + student.history
print("|{:^12s}|{:^12s}|{:^12d}|{:^12d}|{:^12d}|{:^12d}|{:^12d}|{:^12d}|".format(student.id, student.name, student.chinese, student.math,student.english, student.physics, student.history, total_score))
print("-" * 106)
运行结果:
五、结语
1.第一版本和第二版本改进的地方区别在于txt文件,把它作为存储数据,第二版本有了数据保存性,减少添加学生信息的重复操作,省时省力。
2.缺点:代码信息貌似有些冗余…
3.展望,如果学生信息管理的面向用户是谁呢?老师?
希望有一个不同的老师管理班级学生,提供每一个老师的权限。应当设置一个账号和密码?
如果老师希望看单个科目的最高成绩是谁?最低成绩是谁?总平均分数是多少?
第一版源代码链接:Daybreak/Python_Res/学生信息管理系统v1.0 - 码云 - 开源中国 (gitee.com)
第二版源代码链接:Daybreak/Python_Res/学生信息管理系统v2.0 - 码云 - 开源中国 (gitee.com)