删除和修改操作未完成
import row
import xlwt
import os
from openpyxl import *
# 构建workbook对象
workbook = load_workbook('stuinfo.xlsx')
def main():
while True:
menu()
choice = int(input('请选择'))
if choice in [0, 1, 2, 3, 5, 6, 7, 8, 9, 10]:
if choice == 0:
answer = input('您确定要退出系统吗?y/n')
if answer == 'y' or answer == 'Y':
print('感谢您的使用')
break
else:
continue
elif choice == 1:
insert()
elif choice == 2:
total()
elif choice == 3:
serch(read_excel_data())
# elif choice == 4:
# delete()
elif choice == 5:
modify()
elif choice == 6:
sort(read_excel_data())
elif choice == 7:
show()
elif choice == 8:
all_avg(read_excel_data())
elif choice == 9:
subject_avg(read_excel_data())
elif choice == 10:
highest(read_excel_data())
def menu():
print('功能菜单')
print('0.退出')
print('1.录入学生信息')
print('2.查询学生总数')
print('3.按姓名查找')
print('4.删除学生信息')
print('5.修改学生信息')
print('6.按平均分排序')
print('7.显示所有学生成绩')
print('8.查所有学生平均分')
print('9.每科平均分')
print('10.查询每科最高分')
def insert():
student_list = []
while True:
name = input('请输入姓名:')
if not name:
break
try:
english = int(input('请输入英语成绩'))
python = int(input('请输入Python成绩'))
java = int(input('请输入java成绩'))
except:
print('输入无效,请重新输入')
continue
# 将学生信息保存到列表
stu_info = [name, english, python, java]
print(stu_info)
#将学生信息保存到字典中
# student = {'name': name, 'english': english, 'python': python, 'java': java}
# print(student)
#将学生信息添加到列表中
student_list.append(stu_info)
# print(student_list)
# 将从界面输入的数据封装成列表
stu_info = [name, english, python, java]
sheet = workbook.active
row = sheet.max_row + 1
column = sheet.max_column + 1
for data_cell in range(1, column):
sheet.cell(row=row, column=data_cell, value=stu_info[data_cell-1])
workbook.save(filename='stuinfo.xlsx')
answer = input('是否继续添加? y/n \n')
if answer == 'y':
continue
else:
break
#调用save()函数
# save(student_list)
print('学生信息录入完毕')
# print(student_list)
def save(lst):
pass
# try:
# stu_txt = open(filename, 'a', encoding='utf-8')
# except:
# stu_txt = open(filename, 'w', encoding='utf-8')
# for item in lst:
# stu_txt.write(str(item)+'\n')
# stu_txt.close()
def total():
read_excel_data()
work_book = load_workbook('stuinfo.xlsx')
sheet = work_book.active
stuinfo_list = []
# print(sheet.max_row)
print('一共有' + str(sheet.max_row) + '名学生')
def serch(score):
a = input("请输入学生姓名:")
flag = False
for s in score:
if a == s[0]:
flag = True
print('姓名:%s 英语成绩:%.1f Python成绩:%.1f Java成绩:%.1f '
% (s[0], s[1], s[2], s[3]))
break
if flag == False:
print("未查询到该学生信息")
# student_query = []
# while True:
# id = ''
# name = ''
# if os.path.exists('stuinfo.xlsx'):
# mode = input('按id查找请输入1,按姓名查找请输入2:')
# if mode == '1':
# id = input('请输入学生id')
# elif mode == '2':
# name = input('请输入学生姓名:')
# else:
# print('您的输入有误,请重新输入')
# serch()
# rfile = read_excel_data()
# print(rfile)
# # for item in student:
# # d = dict(eval(item))
# # if id != '':
# # if d['id'] == id:
# # student_query.append(d) # 查到的学生添加到列表里
#