1.需求分析
2.系统设计
3.系统开发必备
4.主函数设计
5.学生信息维护模块设计
6.查询/统计模块设计
7.排序模块设计
8.项目打包
1.录入学生信息
2.查找学生信息
3.删除学生信息
4.修改学生信息
5.排序
6.统计学生总人数
7.显示所有学生信息
0.退出系统
系统开发环境:
操作系统:window10
Python解释器版本:Python3.7
开发工具:Pycharm
Python内置模块:os,re
项目目录结构:
main.py
menu.py
operate.py
save.py
display.py
在主函数中通过调用各个函数实现需求分析内容
1.录入学生信息 insert()
2.查找学生信息 find()
3.删除学生信息 delete()
4.修改学生信息 modify()
5.排序 sort()
6.统计学生总人数 total()
7.显示所有学生信息 show()
0.退出系统
from menu import menu
import operate
def main():
while 1:
menu()
choice = int(input('请输入你需要的功能:'))
if choice in range(0,8):
if choice == 0:
answer = input('你确定要退出系统吗?y/n')
if answer in ('y','Y'):
print('谢谢你的使用')
break
else:
continue
elif choice == 1:
operate.insert()
elif choice == 2:
operate.find()
elif choice == 3:
operate.delete()
elif choice == 4:
operate.modify()
elif choice == 5:
operate.sort()
elif choice == 6:
operate.total()
elif choice == 7:
operate.show()
if __name__ == '__main__':
main()
def menu():
print('学生信息管理系统'.center(50,'='))
print('功能菜单'.center(50,'-'))
print('\t\t\t\t\t1.录入学生信息')
print('\t\t\t\t\t2.查找学生信息')
print('\t\t\t\t\t3.删除学生信息')
print('\t\t\t\t\t4.修改学生信息')
print('\t\t\t\t\t5.排序')
print('\t\t\t\t\t6.统计学生总人数')
print('\t\t\t\t\t7.显示所有学生信息')
print('\t\t\t\t\t0.退出系统')
print('-'.center(50,'-'))
# Author : zxy
# Date : 2022/5/21 23:53
import display
import save
import os
def insert(filename):
student_list = []
while True:
id = input('请输入ID(如1001):')
if not id:
break
name = input('请输入姓名:')
if not name:
break
try:
english = int(input('请输入英语成绩:'))
python = int(input('请输入Python成绩:'))
java = int(input('请输入Java成绩:'))
except:
print('输入有误!!请重新输入!!')
continue
# 将信息报错在字典中
student = {'id':id,'name':name,'english':english,'python':python,'java':java}
student_list.append(student)
answer = input('是否继续添加?y/n\n')
if answer in ('y','Y'):
continue
else:
break
save.save(student_list,filename)
def find(filename):
student_lst = []
while True:
id = ''
name = ''
if os.path.exists(filename):
mode = int(input('按ID查找请输入1,按姓名查找请输入2:'))
if mode == 1:
id = input('请输入学生ID:')
elif mode == 2:
name = input('请输入学生姓名:')
else:
print('你输入有误,请重新输入!!!')
find(filename)
with open(filename,'r',encoding='utf-8') as rfile:
students = rfile.readlines()
for student in students:
dic = dict(eval(student))
if id != '':
if dic['id'] == id:
student_lst.append(dic)
elif name != '':
if dic['name'] == name:
student_lst.append(dic)
display.show_student(student_lst)
student_lst.clear()
answer = input('是否继续查询?y/n')
if answer in ('y','Y'):
continue
else:
break
else:
print('没有学生信息!!!')
def delete(filename):
while True:
student_id = input('请输入要删除的学生的ID:')
if student_id != '':
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
student_old = rfile.readlines()
else:
student_old = []
flag = False
if student_old:
with open(filename,'w',encoding='utf-8') as wfile:
dic = {}
for item in student_old:
dic = dict(eval(item))
if dic['id'] != student_id:
wfile.write(str(dic)+'\n')
else:
flag = True
if flag:
print('id为{}的学生信息已被删除'.format(student_id))
else:
print(f'没有找到ID为{student_id}的学生信息')
else:
print('没有学生信息')
break
show(filename)
answer = input('是否继续删除?y/n' + '\n')
if answer in ('y','Y'):
continue
else:
break
def modify(filename):
while True:
answer = input('请输入要修改的学生ID:')
if answer != '':
if os.path.exists(filename) :
with open(filename,'r',encoding='utf-8') as rfile:
student_old = rfile.readlines()
else:
student_old = []
flag = False
if student_old:
with open(filename,'w',encoding='utf-8') as wfile:
dic = {}
for student in student_old:
dic = dict(eval(student))
if dic['id'] == answer:
print('找到这个名学生,可以修改他的相关信息了')
name = input('请输入姓名:')
dic['name'] = name
english = input('请输入英语成绩:')
dic['english'] = english
python = input('请输入Python成绩:')
dic['python'] = python
java = input('请输入Java成绩:')
dic['java'] = java
wfile.write(str(dic)+'\n')
else:
wfile.write(str(dic)+'\n')
flag = True
if not flag:
print('ID为{0}的学生信息修改成功'.format(answer))
else:
print('你输入的学生ID:{}不存在!!!'.format(answer))
else:
print('没有学生信息!!!')
break
show(filename)
answer = input('是否继续修改其他学生信息?y/n'+'\n')
if answer in ('y','Y'):
continue
else:
break
else:
print('请输入正确的学生ID')
def sort(filename):
show(filename)
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students = rfile.readlines()
student_new = []
for student in students:
dic = dict(eval(student))
student_new.append(dic)
else:
print('未录入学生信息!!!')
asc_or_desc = input('请选择(0升序,1降序):')
if asc_or_desc == '0':
asc_or_desc_bool = False
elif asc_or_desc == '1':
asc_or_desc_bool = True
else:
print('你输入有误,请重新输入!!!')
sort(filename)
mode = input('请选择排序方式(1.按英语排序,2.按Python排序,3.按Java排序,0.按总成绩排序):')
if mode == '1':
student_new.sort(key=lambda x : int(x['english']),reverse=asc_or_desc_bool)
elif mode == '2':
student_new.sort(key=lambda x : int(x['python']),reverse=asc_or_desc_bool)
elif mode == '3':
student_new.sort(key=lambda x : int(x['java']),reverse=asc_or_desc_bool)
elif mode == '5':
student_new.sort(key=lambda x : int(x['english']) + int(x['python']) + int(x['java']) ,reverse=asc_or_desc_bool)
else:
print('你的输入有误,请重新输入!!!')
sort(filename)
display.show_student(student_new)
def total(filename):
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students = rfile.readlines()
if students != '':
print('一共有{}名学生'.format(len(students)))
else:
print('学生信息为空!!!')
else:
print('暂未报错学生信息!!!')
def show(filename):
student_lst = []
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students = rfile.readlines()
for student in students:
dic = dict(eval(student))
student_lst.append(dic)
display.show_student(student_lst)
student_lst.clear()
else:
print('暂未保存学生信息!!!')
def save(lst,filename):
try:
stu_data = open(filename,'a',encoding='utf-8')
except:
stu_data = open(filename,'w',encoding='utf-8')
for item in lst:
stu_data.write(str(item)+'\n')
stu_data.close()
def show_student(lst):
if len(lst) == 0:
print('没有查询到学生信息,无数据显示!!!')
return
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}\t'
print(format_title.format('ID','姓名','英语成绩','Python成绩','Java成绩','总成绩'))
format_data = '{:^6}\t{:^12}\t{:^10}\t{:^10}\t{:^10}\t{:^10}\t'
for student in lst:
print(format_data.format(student.get('id'),
student.get('name'),
student.get('english'),
student.get('python'),
student.get('java'),
int(student.get('english')) + int(student.get('python')) + int(student.get('java'))
))
安装:pip install PyInstaller
单文件打包:
pyinstaller -F main.py [打包exe]
pyinstaller -F -w main.py [不带控制台打包]
pyinstaller -F -i main.ico main.py [指定图标打包]
多文件打包:
1.pyi-makespec main.py(将主函数生成对应的spec文件)
2.修改spec文件,i按察Analysis下是否包含所有py文件,检查pathex的项目路径是否正确
3.pyinstaller main.spec(对spec文件进行pyinstaller安装)
Analysis:
['main.py','menu.py','operate.py','display.py','save.py'],<将所有需要打包py文件写入>
pathex=['E:\\BigData\\date\\PyCharm\\May2\\project'],<写入正确的项目路径>
datas=[('date\\student.txt','date'),('icon.ico','icon')],<首先是项目中文件,其次是打包后文件夹名称>
exe:
name='学生信息管理系统',<指定exe名称>
icon='icon.ico',<指定图标打包>
图片转ico图标工具