(一):程序介绍和流程图
(二):主要文件main.py
(三):基础文件basics.py
(四):管理学生信息manage.py
(五):查询学生信息query.py
(六):导入数据文件import_data.py
(七):导出数据文件export_data.py
因为之前已经导出全部信息,全部信息包含了学生信息,所以不能导出。
注:导出部分两个选项的区别主要在于参数的不同,除此之外是差不多的,于是这个只举了一个例子。
def main( cn):
“”“判断是否有学生存在,并根据选项调用new和old函数”""
def main_show():
“”“主函数的显示”""
def new( cn):
“”“写入全新的文件”""
def old( cn):
“”“追加写入已有文件”""
def newold_show():
“”“用于new和old的显示”""
def write_all( cn, file, count):
“”“导出全部信息到xls文件中”""
def write_students( cn, file, count):
“”“导出学生信息到xls文件中”""
def write_courses( cn, file, count):
“”“导出课程信息到xls文件中”""
def write_reports( cn, file, count):
“”“导出成绩信息到xls文件中”""
def write_sort( cn, file, count):
“”“导出排序信息到xls文件中”""
def sort_show():
“”“write_sort的显示函数”""
def write_total( cn, file, count):
“”“按总分排序查看”""
def write_single( cn, file, count):
“”“按单科成绩排序”""
def single( cn, file, cname, count):
“”“按write_single的选择导入数据”""
def cname_to_cno( cn, cname):
“”“将cname转换成cno”""
import os
import xlwt, xlrd
from xlutils.copy import copy
import basics as ba
def newold_show():
"""用于new和old的显示"""
print("****************************")
print("\t 1:导出全部信息")
print("\t 2:导出学生信息")
print("\t 3:导出课程信息")
print("\t 4:导出成绩信息")
print("\t 5:导出排序信息")
print("\t 0:返回导出菜单")
print("****************************\n")
def write_all( cn, file, count):
"""导出全部信息到xls文件中"""
write_students( cn, file, count)
write_courses( cn, file, 1)
write_reports( cn, file, 1)
print("全部信息到入%s完毕" % file)
def write_students( cn, file, count):
"""导出学生信息到xls文件中"""
if count == 0:
new_workbook = xlwt.Workbook()
new_sheet = new_workbook.add_sheet("Students")
else:
wb = xlrd.open_workbook( file,formatting_info=True)
newb = copy(wb)
try:
new_sheet = newb.add_sheet("Students")
except Exception:
print("已经存在Students表,请重新输入")
return
# 写名称,在表格的第一行
new_sheet.write(0,0,"Sno")
new_sheet.write(0,1,"Sname")
new_sheet.write(0,2,"Sgender")
new_sheet.write(0,3,"Sbirth")
new_sheet.write(0,4,"Sclass")
new_sheet.write(0,5,"Snative")
sql = '''SELECT DISTINCT Sno, Sname, Sgender,
Sbirth, Sclass, Snative
FROM Students
ORDER BY Sno ASC
'''
information = ba.common( cn, sql)
for i, item in enumerate( information, start=1):
for j in range( 0, 6):
new_sheet.write( i, j, item[j])
if count == 0:
new_workbook.save( file)
else:
newb.save( file)
print("学生信息到入%s完毕" % file)
def write_courses( cn, file, count):
"""导出课程信息到xls文件中"""
if count == 0:
new_workbook = xlwt.Workbook()
new_sheet = new_workbook.add_sheet("Courses")
else:
wb = xlrd.open_workbook( file,formatting_info=True)
newb = copy(wb)
try:
new_sheet = newb.add_sheet("Courses")
except Exception:
print("已经存在Courses表,请重新输入")
return
# 写名称,在表格的第一行
new_sheet.write(0,0,"Cno")
new_sheet.write(0,1,"Cname")
new_sheet.write(0,2,"Chours")
new_sheet.write(0,3,"Ccredit")
sql = '''SELECT DISTINCT Cno, Cname,
Chours, Ccredit
FROM Courses
ORDER BY Cno ASC
'''
information = ba.common( cn, sql)
for i, item in enumerate( information, start=1):
for j in range( 0, 4):
new_sheet.write( i, j, item[j])
if count == 0:
new_workbook.save( file)
else:
newb.save( file)
print("课程信息到入%s完毕" % file)
def write_reports( cn, file, count):
"""导出成绩信息到xls文件中"""
if count == 0:
new_workbook = xlwt.Workbook()
new_sheet = new_workbook.add_sheet("Reports")
else:
wb = xlrd.open_workbook( file,formatting_info=True)
newb = copy(wb)
try:
new_sheet = newb.add_sheet("Reports")
except Exception:
print("已经存在Reports表,请重新输入")
return
# 写名称,在表格的第一行
new_sheet.write(0,0,"Sno")
new_sheet.write(0,1,"Cno")
new_sheet.write(0,2,"Racademicyear")
new_sheet.write(0,3,"Rterm")
new_sheet.write(0,4,"Grade")
sql = '''SELECT DISTINCT Sno, Cno,
Racademicyear, Rterm, Grade
FROM Reports
ORDER BY Sno ASC, Cno ASC
'''
information = ba.common( cn, sql)
for i, item in enumerate( information, start=1):
for j in range( 0, 5):
new_sheet.write( i, j, item[j])
if count == 0:
new_workbook.save( file)
else:
newb.save( file)
print("成绩信息到入%s完毕" % file)
# 导出排序信息
def write_total( cn, file, count):
"""按总分排序查看"""
if count == 0:
new_workbook = xlwt.Workbook()
new_sheet = new_workbook.add_sheet("Total")
else:
wb = xlrd.open_workbook( file,formatting_info=True)
newb = copy(wb)
try:
new_sheet = newb.add_sheet("Total")
except Exception:
print("已经存在Total表,请重新输入")
return
sql = '''SELECT DISTINCT Racademicyear,
Rterm, Sno, SUM(Grade)
FROM Reports
GROUP BY Sno, Racademicyear, Rterm
ORDER BY Racademicyear DESC, Rterm DESC,
SUM(Grade) DESC
'''
information = ba.common( cn, sql)
new_sheet.write(0,1,"Racademicyear")
new_sheet.write(0,2,"Rterm")
new_sheet.write(0,3,"Sno")
new_sheet.write(0,4,"Total")
i = 1
y = 0
for item in information:
if y != item[0]:
y = item[0]
i = i + 1
new_sheet.write( i, 0, str(item[0]) + '年度')
i = i + 1
m = 0
for j in range( 1, 5):
new_sheet.write( i, j, item[m])
m = m + 1
if m == 4:
m = 0
i = i + 1
if count == 0:
new_workbook.save( file)
else:
newb.save( file)
print("成绩信息到入%s完毕" % file)
def cname_to_cno( cn, cname):
"""将cname转换成cno"""
sql = '''SELECT DISTINCT Cno
FROM Courses
WHERE Cname = "%s"
''' % cname
cursor = cn.execute( sql)
ccno = []
for row in cursor:
ccno.append( row)
cno = ccno[0]
return cno
def single( cn, file, cname, count):
"""按write_single的选择导入数据"""
if count == 0:
new_workbook = xlwt.Workbook()
new_sheet = new_workbook.add_sheet(cname)
else:
wb = xlrd.open_workbook( file,formatting_info=True)
newb = copy(wb)
try:
new_sheet = newb.add_sheet( cname)
except Exception:
print("已经存在%s表,请重新输入" %
cname)
return
cno = cname_to_cno( cn, cname)
sql = '''SELECT DISTINCT Racademicyear,
Rterm, Sno, Grade
FROM Reports WHERE Cno = "%s"
ORDER BY Racademicyear DESC, Rterm DESC,
Grade DESC
''' % cno
information = ba.common( cn, sql)
new_sheet.write(0,1,"Racademicyear")
new_sheet.write(0,2,"Rterm")
new_sheet.write(0,3,"Sno")
new_sheet.write(0,4,"Grade")
i = 1
y = 0
for item in information:
if y != item[0]:
y = item[0]
i = i + 1
new_sheet.write( i, 0, str(item[0]) + '年度')
i = i + 1
m = 0
for j in range( 1, 5):
new_sheet.write( i, j, item[m])
m = m + 1
if m == 4:
m = 0
i = i + 1
if count == 0:
new_workbook.save( file)
else:
newb.save( file)
print("成绩信息到入%s完毕" % file)
def write_single( cn, file, count):
"""按单科成绩排序"""
sql = '''SELECT Cname
FROM Courses
'''
total_cname = ba.common( cn, sql)
print("存在以下课程:")
i = 0
for row in total_cname:
print( row[0], end = " ")
i = i + 1
if i % 5 == 0:
print("")
print("")
cname = input("\n请输入课程名:")
judge = ba.judge_comment\
( cn, 'Courses', 'Cno', 'Cname', cname)
if judge == 0:
print("课程不存在,请重新输入")
write_single( cn, file, count)
else:
cno = ba.cname_to_cno( cn, cname)
single( cn, file, cname, count)
confirm = input("\n请问是否继续'按单科成绩导入'(Y/N)")
if confirm == 'n' or confirm == 'N':
return
else:
write_single( cn, file, 1)
def sort_show():
"""write_sort的显示函数"""
print("****************************")
print("\t 1:按总分排序导入")
print("\t 2:按单科成绩导入")
print("\t 0:返回上一个菜单")
print("****************************\n")
def write_sort( cn, file, count):
"""导出排序信息到xls文件中"""
while 1:
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
sort_show()
m42 = input("请输入选项:")
if m42 == '1': #按总分排序导入
write_total( cn, file, count)
count = 1
elif m42 == '2': #按单科成绩导入
write_single( cn, file, count)
count = 1
elif m42 == '0':
return
else:
print("无效的命令,请重新输入")
#被主函数调用的两个函数
def new( cn):
"""写入全新的文件"""
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
file = ba.judge_file()
#调用函数“检查学生成绩记录文件是否创建”
if not os.path.exists(file):
m = input("文件不存在,是否创建?(Y/N) ")
if m == 'Y' or m == 'y':
ba.create_file( file)
else:
return
else:
m = input("文件已经存在,是否覆盖?(Y/N) ")
if m == 'Y' or m == 'y':
os.remove( file)
ba.create_file( file)
else:
return
count = 0
while 1:
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
newold_show()
m41 = input("请输入选项:")
if m41 == '1': #导出全部信息
if count == 0:
write_all( cn, file, 0)
count = 1
else:
write_all( cn, file, 1)
elif m41 == '2': #导出学生信息
if count == 0:
write_students( cn, file,0)
count = 2
else:
write_students( cn, file,1)
elif m41 == '3': #导出课程信息
if count == 0:
write_courses( cn, file, 0)
count = 2
else:
write_courses( cn, file, 1)
elif m41 == '4': #导出成绩信息
if count == 0:
write_reports( cn, file, 0)
count = 2
else:
write_reports( cn, file, 1)
elif m41 == '5': #导出排序信息
if count == 0:
write_sort( cn, file, 0)
count = 2
else:
write_sort( cn, file, 1)
elif m41 == '0':
return
else:
print("无效的命令,请重新输入")
def old( cn):
"""追加写入已有文件"""
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
i = os.system("cls")
file = ba.judge_file()
#调用函数“检查学生成绩记录文件是否创建”
if not os.path.exists(file):
m = input("文件不存在,请返回上一步 ")
return
while 1:
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
newold_show()
m42 = input("请输入选项:")
if m42 == '1': #导出全部基本信息
write_all( cn, file, 1)
elif m42 == '2': #导出学生信息
write_students( cn, file,1)
elif m42 == '3': #导出课程信息
write_courses( cn, file, 1)
elif m42 == '4': #导出成绩信息
write_reports( cn, file, 1)
elif m42 == '5': #导出排序信息
write_sort( cn, file, 1)
elif m42 == '0':
return
else:
print("无效的命令,请重新输入")
#主函数
def main_show():
"""主函数的显示"""
print("****************************")
print(" 导出菜单")
print("****************************")
print("\t 1:写入全新的文件")
print("\t 2:追加写入已有文件")
print("\t 0:返回主菜单")
print("****************************\n")
def main( cn):
"""判断是否有学生存在,并根据选项调用函数"""
sql = '''SELECT DISTINCT Sno
FROM Students
'''
stus = ba.common( cn, sql)
count = 0
for row in stus:
count = count + 1
if count == 0:
print("数据库中没有学生,"
"请添加学生或导入数据")
i = input("\n按下回车键后,将清空屏幕,"
"返回主菜单")
i = os.system("cls")
return
while 1:
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
main_show()
m4 = input("请输入选项:")
if m4 == '1': #写入全新的文件
new( cn)
elif m4 == '2': #追加写入已有文件
old( cn)
elif m4 == '0':
return
else:
print("无效的命令,请重新输入")
如果文章对你有帮助,点赞是对我最好的鼓励了!