写在前面
大家好,我是第一次python学了一个学期,期末要完成一个毕业生信息管理系统大作业的小韩了,由于上次没有仔细看开发实现的要求,实现了一个简单的毕业生信息管理系统,而这次专门整理了两种使用文件进行保存数据实现的毕业生信息管理系统,因为是第一次学python,还不太熟悉python的写法, 而之前是学 c 、c++,感觉我的这个写的有一股 c/c++的内味:
1. 使用excel .xlsx 保存数据实现一个毕业生信息管理系统
2. 使用文本文档.txt保存数据实现一个毕业生信息管理系统
以下将会在代码进行详细的介绍
一、 对excel表格操作实现一个毕业生信息管理系统
开发要求
1. 采用 python 语言实现一个XX信息管理系统
2.实现基本的增删改查的基本功能,还可以加上一些如排序,搜索的操作
3. 采用文件保存数据(而不是每次从键盘输入)
4. 各个功能界面循环调用
环境以及开发软件
1. python 3.7.0 版本
2. pycharm 2019 中文版
一、 函数模块设计:
1.主函数模块
实现功能
查询搜索毕业生信息列表
增加毕业生信息
修改毕业生信息
删除毕业生信息
毕业生信息排序
退出毕业生信息管理系统
def main ( ) :
arry = [ 0 , 1 , 2 , 3 , 4 , 5 ]
Menu( )
while 1 :
a = input ( "请输入: " )
if a. isdigit( ) and int ( a) in arry:
a = int ( a)
while a:
if a == 1 :
PrintStudentList( )
Menu( )
break
if a == 2 :
AddStudent( )
Menu( )
break
if a == 3 :
ChangeStudent( )
Menu( )
break
if a == 4 :
DeleteStudent( )
Menu( )
break
if a == 5 :
SortData( )
Menu( )
break
elif a > 5 or a < 0 :
print ( "输入有误!" )
break
if a == 0 :
print ( "系统已退出!" )
exit( )
else :
print ( "请输入0--5!" )
main( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
38 39 40
这里因为还没有学到python中的字典那部分知识,而pyhton中又没有switch和case所以就使用这个 if 进行判断 虽然比较繁琐,但是看起来还是比较清晰易懂的
二、 数据文件设计
因为这里要采用文件进行保存数据,我第一个想到的就是excel表格,这种.xlsx文件保存数据一目了然,因此本次选择了excel表格进行数据保存,写入,读取,修改,删除等基本功能
主要信息:
本次实现的是一个毕业生信息管理系统,因此给每个毕业生设计的个人信息如下:
学号 姓名 电话 年级 学院 就业 就业公司 邮箱 月薪
关于对excel 表格使用则需要导入两个包:
from openpyxl import Workbook
from openpyxl import load_workbook
如果这里导入失败的话,可能需要自己手动进行
openpyxl
的下载 下载方法具体如下
点击 文件 -->> 点击设置(setting) -->> 点击自己的项目 -->> 点击第一个选项 -->> 点击页面的右侧的加号 -->> 输入 想要导入的包 (openpyxl) -->> 点击左下角的 install Package 稍等一会即可完成安装
2.增加毕业生信息模块
从键盘获取输入的信息,信息校验成功后,先将信息保存在一个列表中,然后最后将整个列表插入到excel表格中,然后保存,这样方便写入,否则频繁的打开关闭文件比较繁琐,容易出现错误。
例如:下面插入 学号 id
先建立一个空的列表 然后先对学号进行校验,首先校验学号是否合法,比如长度要求,或者插入的是否和表中的是否重复,当校验成功后才将学号保存到 列表中
r = [ ]
ID = None
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
id = input ( "请输入学号:" )
if CheckIdIsRight( id ) :
while 1 :
id = input ( "请输入正确的学号!" )
if not CheckIdIsRight( id ) :
ID = id
break
else :
ID = id
r. append( ID)
其余的其他信息依次类推
最后将整个列表插入到excel表格中,然后关闭并保存文件
sheet. append( r)
wb. close( )
wb. save( 'StudentList.xlsx' )
3. 查询搜索毕业生信息模块
该功能主要实现查询和搜索的功能 ,比如查看所有信息列表 ,按相关信息查询毕业生信息,
例如:查询所有毕业生信息:
def PrintAll ( ) :
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
for row in sheet. rows:
for cell in row:
print ( cell. value, end= " " )
print ( )
print ( )
只需要将每一个单元格的按顺序打印出来即可
例如:按学号查询该毕业生的信息
def SelectById ( ) :
id = input ( "请输入需要查询的学号:"
)
if id . isdigit( ) and not CheckIdIsRight( id ) :
id1 = int ( id )
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
r = FindId( id1)
for i in range ( 1 , 10 ) :
print ( sheet. cell( 1 , i) . value, end= " " )
print ( )
for i in range ( 1 , 10 ) :
print ( sheet. cell( r, i) . value, end= " " )
print ( )
else :
print ( "学号输入错误!" )
首先应该判断一下输入的学号是不是一串数字,而且 想要查询的学生的学号是存在的,因为我们这里规定学号应该是类似于1700000000 这样的一个数字,而python默认 input 输入的是一个 str 字符串类型的 所以这里防止输入错误导致程序崩溃 因此加入了一些校验,当确认合法之后 再将其 转换为 int 类型的变量 进行使用。
而具体的就是通过一个findid的函数来返回学号所在的行 这样就可以将这一行的信息打印出来即可 ,打印学生信息的同时不要忘了打印表头的信息,这样看起来会更加的清晰。
4. 修改毕业生信息模块
在修改该学生信息之前,同样对其输入的学号进行校验,校验完成之后进行相关信息的修改
修改的基本方法就是,通过学号找到该学生所在的行,然后对特定的列的信息修改(直接赋值),最后保存到文件即可
例如 : 修改姓名
def changename ( row, wb) :
name = input ( "请输入修改之后的名字:" )
sheet. cell( row, 2 , name)
wb. save( 'StudentList.xlsx' )
5. 毕业生信息排序
这里排序主要使用了一个冒泡排序的算法 对数据进行排序,虽然python中是有内置的排序算发法的,但是我这里还是自己实现了一个排序(升序),排完序了之后 也可以对升序的进行一个反转 得到一个降序的列表。 因为是对某一项的单一数据进行排序,而排序结果是要求打印出所有信息的,因此先得到一个某一项数据排好序的列表,然后将列表对应的信息进行打印即可。
例如: 按学号进行排序
冒泡排序:
def BubbleSort ( l2) :
for i in range ( 0 , len ( l2) ) :
count = 0
for j in range ( 1 , len ( l2) - i) :
if int ( l2[ j - 1 ] ) > int ( l2[ j] ) :
temp = l2[ j]
l2[ j] = l2[ j - 1 ]
l2[ j - 1 ] =
temp
count = count + 1
if count == 0 :
return l2
return l2
按学号从小到大排序并打印学生信息
def GetAllStudentById ( ) :
l = [ ]
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
for column in list ( sheet. columns) [ 0 ] :
l. append( column. value)
l2 = l[ 1 : ]
l3 = BubbleSort( l2)
for i in range ( 1 , 10 ) :
print ( sheet. cell( 1 , i) . value, end= " " )
print ( )
for i in range ( 0 , len ( l3) ) :
r = FindId( l3[ i] )
for j in range ( 1 , 10 ) :
print ( sheet. cell( r, j) . value, end= " " )
print ( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
注意 :因为学号是唯一的,因此可以通过找道该行,然后通过行号进行某一行的定向信息打印,但是像年级 ,月薪信息是有可能重复的,就不能再像学号一样查找,打印了,但是我们可以先将年级的列表排好序,然后进行一个去重,这样,就可以将符合满足,排好序的年级列表中的年级对应的学生,信息全部打印出来
6. 删除毕业生信息
非常简单,只需要将要删除的学生的学号输入,然后学号校验合法且存在之后,找到对应的该行,然后将这一行的数据删除就可以了。
def DeleteStudent ( ) :
PrintAll( )
id = input ( "请输入要删除学生的学号:" )
if not CheckIdIsRight( id ) :
print ( "学号正确!" )
id = int ( id )
row = FindId( id )
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
isdelete = input ( "是否删除该学生信息?输入是或否:" )
if isdelete == '是' :
sheet. delete_rows( row, 1 )
wb. save(
'StudentList.xlsx' )
print ( "删除成功!" )
else :
print ( "删除失败!" )
else :
print ( "学号输入错误!" )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
三、 测试
1. 查询搜索测试
2. 添加测试
3.修改测试
4. 信息排序测试
5. 删除信息测试
四、 源码
from openpyxl import Workbook
from openpyxl import load_workbook
IsJob = [ '是' , '否' ]
def Menu ( ) :
print ( end= " " * 45 )
print ( '*' * 22 )
print ( end= " " * 45 )
print ( "* 查询毕业生信息输入: 1 *" )
print ( end= " " * 45 )
print ( "* 添加毕业生信息输入: 2 *" )
print ( end= " " * 45 )
print ( "* 修改毕业生信息输入: 3 *" )
print ( end= " " * 45 )
print ( "* 删除毕业生信息输入: 4 *" )
print ( end= " " * 45 )
print ( "* 查看排序统计请输入: 5 *" )
print ( end= " " * 45 )
print ( "* 退出系统请输入: 0 *" )
print ( end= " " * 45 )
print (
'*' * 22 )
def SelectStudentMenu ( ) :
print ( end= " " * 45 )
print ( '*' * 25 )
print ( end= " " * 45 )
print ( "* 查看所有信息请输入: 1 *" )
print ( end= " " * 45 )
print ( "* 按学号查询信息输入: 2 *" )
print ( end= " " * 45 )
print ( "* 按年级查询信息输入: 3 *" )
print ( end= " " * 45 )
print ( "* 按是否就业查询输入: 4 *" )
print ( end= " " * 45 )
print ( "* 退出查询功能请输入: 0 *" )
print ( end= " " * 45 )
print ( '*' * 25 )
def FindId ( id ) :
i = 0
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
for column in list ( sheet. columns) [ 0 ] :
i = i + 1
if id == column. value:
return i
def BubbleSort ( l2) :
for i in range ( 0 , len ( l2) ) :
count = 0
for j in range ( 1 , len ( l2) ) :
if int ( l2[ j - 1 ] ) > int ( l2[ j] ) :
temp = l2[ j]
l2[ j] = l2[ j - 1 ]
l2[ j - 1 ] = temp
count = count + 1
if count == 0 :
return l2
return l2
def GetAllStudentByGadeOrMoney ( x) :
l = [ ]
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
for column in list ( sheet. columns) [ x] :
l. append( column. value)
l2 = l[ 1 : ]
l3 =
BubbleSort( l2)
i = 1
l3. reverse( )
while i < len ( l3) :
if l3[ i] == l3[ i - 1 ] :
del l3[ i - 1 ]
else :
i = i + 1
for i in range ( 1 , 10 ) :
print ( sheet. cell( 1 , i) . value, end= " " )
print ( )
j = 0
while j < len ( l3) :
for row in sheet. rows:
for cell in row:
if cell. value == l3[ j] :
for cell in row:
print ( cell. value, end= " " )
print ( )
j = j + 1
print ( )
def GetAllStudentById ( ) :
l = [ ]
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
for column in list ( sheet. columns) [ 0 ] :
l. append( column. value)
l2 = l[ 1 : ]
l3 = BubbleSort( l2)
for i in range ( 1 , 10 ) :
print ( sheet. cell( 1 , i) . value, end= " " )
print ( )
for i in range ( 0 , len ( l3) ) :
r = FindId( l3[ i] )
for j in range ( 1 , 10 ) :
print ( sheet. cell( r, j) . value, end= " " )
print ( )
def PrintAll ( ) :
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
for row in sheet.
rows:
for cell in row:
print ( cell. value, end= " " )
print ( )
print ( )
def PrintStudentList ( ) :
def SelectById ( ) :
id = input ( "请输入需要查询的学号:" )
if id . isdigit( ) and not CheckIdIsRight( id ) :
id1 = int ( id )
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
r = FindId( id1)
for i in range ( 1 , 10 ) :
print ( sheet. cell( 1 , i) . value, end= " " )
print ( )
for i in range ( 1 , 10 ) :
print ( sheet. cell( r, i) . value, end= " " )
print ( )
else :
print ( "学号输入错误!" )
def SelectByGrade ( ) :
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
grade = input ( "请输入要查询的年级:" )
if grade. isdigit( ) :
for i in range ( 1 , 10 ) :
print ( sheet. cell( 1 , i) . value, end= " " )
print ( )
for row in sheet. rows:
for cell in row:
if cell. value == int ( grade) :
for cell in row:
print ( cell. value, end= " " )
print ( )
print ( )
else :
print ( "输入不合法!" )
def SelectByIsJob ( ) :
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
isjob = input ( "请输入要查询的学生是否已经就业 :" )
if isjob in IsJob:
if isjob == '是' :
for i in range ( 1 , 10 ) :
print ( sheet. cell( 1 , i) . value, end= " " )
print ( )
for row in sheet. rows:
for cell in row:
if cell. value == '是' :
for cell in row:
print ( cell. value, end= " " )
print ( )
print ( )
else :
for i in range ( 1 , 10 ) :
print ( sheet. cell( 1 , i) . value, end= " " )
print ( )
for row in sheet. rows:
for cell in row:
if cell. value == '否' :
for cell in row:
print ( cell. value, end= " " )
print ( )
print ( )
else :
print ( "输入错误!" )
arry = [ 0 , 1 , 2 , 3 , 4 ]
while 1 :
SelectStudentMenu( )
a = ( input ( "请输入想要执行的操作:" ) )
if a. isdigit( ) and int ( a) in arry:
a = int ( a)
while a:
if a == 1 :
PrintAll( )
break
if a == 2 :
SelectById( )
break
if a == 3 :
SelectByGrade( )
break
if a == 4 :
SelectByIsJob( )
break
if a < 0 or a > 4 :
print ( "输入错误!请重新输入:" )
break
if a == 0 :
break
else :
print
( "请输入0--4!" )
def CheckIdIsRight ( id1) :
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
if id1. isdigit( ) :
id2 = int ( id1)
for column in list ( sheet. columns) [ 0 ] :
if id2 == column. value:
print ( "学号存在" )
return False
if id2 < 1000000000 or id2 > 10000000000 :
print ( "学号格式不正确!" )
return True
else :
print ( "学号应该是数字!" )
return True
def AddStudent ( ) :
r = [ ]
ID = None
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
id = input ( "请输入学号:" )
if CheckIdIsRight( id ) :
while 1 :
id = input ( "请输入正确的学号!" )
if not CheckIdIsRight( id ) :
ID = id
break
else :
ID = id
r. append( ID)
name = input ( "请输入你的名字:" )
r. append( name)
tell = input ( "请输入你的电话号码:" )
while 1 :
if len ( tell) != 11 :
print ( "电话号码格式不正确!请重新输入:" )
tell = input ( )
if len ( tell) == 11 :
print ( "输入成功!" )
break
if len ( tell) == 11 :
break
r. append( tell)
grade = int ( input ( "请输入你的年级:" ) )
while 1 :
if grade < 2000 :
print ( "年级输入不正确!请重新输入:" )
grade = int ( input ( ) )
if grade >= 2000 :
print ( "输入成功!" )
break
if grade >= 2000 :
break
r. append( grade)
institute = input ( "请输入你的学院:" )
r. append( institute)
isjob = input ( "是否已经工作:输入 :是或否!" )
while 1 :
if isjob in IsJob:
r. append( isjob)
break
else :
print ( "输入错误请重新输入:" )
isjob = input ( )
if r[ 5 ] == '是' :
company = input ( "请输入你的公司名 " )
r. append( company)
else :
company = '无'
r. append( company)
e_mail = input ( "请输入你的电子邮箱:" )
r. append( e_mail)
if r[ 5 ] == '是' :
money = input ( "请输入你的月薪:" )
r. append( money)
if r[ 5 ] == '否' :
money = 0
r. append( money)
sheet. append( r)
wb. close( )
wb. save( 'StudentList.xlsx' )
def StudentPersonalMsg ( ) :
print ( end= " " * 45 )
print ( '*' * 23 )
print ( end= " " * 45 )
print ( "* 修改学生姓名请输入: 1 *" )
print ( end= " " * 45 )
print ( "* 修改电话号码请输入: 2 *" )
print ( end= " " * 45 )
print ( "* 修改是否就业请输入: 3 *" )
print ( end= " " * 45 )
print ( "* 修改就业公司请输入: 4 *" )
print ( end= " " * 45 )
print ( "* 修改邮箱信息请输入: 5 *" )
print ( end= " " * 45 )
print ( "* 修改月薪信息请输入: 6 *" )
print ( end= " " * 45 )
print ( "* 退出修改请输入: 0 *" )
print ( end= " " * 45 )
print ( '*' * 23 )
def ChangeStudent ( ) :
def changename ( row, wb) :
name = input ( "请输入修改之后的名字:" )
sheet.
cell( row, 2 , name)
wb. save( 'StudentList.xlsx' )
def changetell ( row, wb) :
tell = input ( "请输入修改后的电话号码:" )
while 1 :
if len ( tell) != 11 :
print ( "电话号码格式不正确!请重新输入:" )
tell = input ( )
if len ( tell) == 11 :
print ( "输入成功!" )
break
if len ( tell) == 11 :
break
sheet. cell( row, 3 , tell)
wb. save( 'StudentList.xlsx' )
def changeisjob ( row, wb) :
IsJob = [ '是' , '否' ]
isjob = input ( "是否已经工作:输入 :是或否!" )
while 1 :
if isjob in IsJob:
sheet. cell( row, 6 , isjob)
wb. save( 'StudentList.xlsx' )
break
else :
print ( "输入错误请重新输入:" )
isjob = input ( )
def changecompany ( row, wb) :
if sheet. cell( row, 6 ) . value == '是' :
company = input ( "请输入修改后的公司:" )
sheet. cell( row, 7 , company)
wb. save( 'StudentList.xlsx' )
else :
print ( "请先修改是否就业:" )
changeisjob( row, wb)
changecompany( row, wb)
def changemail ( row, wb) :
mail = input ( "请输入修改之后的邮箱:" )
sheet. cell( row, 8 , mail)
wb. save( 'StudentList.xlsx' )
def changemoney ( row, wb) :
if sheet. cell( row, 6 ) . value == '是' :
money = int ( input ( "请输入修改之后的月薪:" ) )
sheet. cell( row, 9 , money)
wb. save( 'StudentList.xlsx' )
else :
print ( "请先修改就业状态及就业公司!" )
changeisjob( row,
wb)
changecompany( row, wb)
changemoney( row, wb)
PrintAll( )
arry = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ]
id = input ( "请输入你要修改的学生的学号:" )
if not CheckIdIsRight( id ) :
print ( "学号正确!" )
row = FindId( id )
wb = load_workbook( 'StudentList.xlsx' )
sheet = wb. active
StudentPersonalMsg( )
while 1 :
a = input ( "请输入:" )
if a. isdigit( ) and int ( a) in arry:
a = int ( a)
while a > 0 :
if a == 1 :
changename( row, wb)
print ( "修改成功!" )
break
if a == 2 :
changetell( row, wb)
print ( "修改成功!" )
break
if a == 3 :
changeisjob( row, wb)
print ( "修改成功!" )
break
if a == 4 :
changecompany( row, wb)
print ( "修改成功!" )
break
if a == 5 :
changemail( row, wb)
print ( "修改成功!" )
break
if a == 6 :
changemoney( row, wb)
print ( "修改成功!" )
break
elif a > 6 or a < 0 :
print ( "输入有误!" )
break
if a == 0 :
break
else :
print ( "请输入正确的选项0--6!" )
break
else :
print ( "请输入正确的学号!" )
def DeleteStudent ( ) :
PrintAll( )
id = input ( "请输入要删除学生的学号:" )
if not CheckIdIsRight( id ) :
print ( "学号正确!" )
id = int ( id )
row = FindId( id )
wb = load_workbook
( 'StudentList.xlsx' )
sheet = wb. active
isdelete = input ( "是否删除该学生信息?输入是或否:" )
if isdelete == '是' :
sheet. delete_rows( row, 1 )
wb. save( 'StudentList.xlsx' )
print ( "删除成功!" )
PrintAll( )
else :
print ( "删除失败!" )
else :
print ( "学号输入错误!" )
def SortMenu ( ) :
print ( end= " " * 45 )
print ( '*' * 30 )
print ( end= " " * 45 )
print ( "* 按学号从小到大排序结果输入: 1 *" )
print ( end= " " * 45 )
print ( "* 按年级从大到小排序结果输入: 2 *" )
print ( end= " " * 45 )
print ( "* 按薪资从高到低排序结果输入: 3 *" )
print ( end= " " * 45 )
print ( "* 退出此功能请输入: 0 *" )
print ( end= " " * 45 )
print ( '*' * 30 )
def SortData ( ) :
SortMenu( )
arry = [ 0 , 1 , 2 , 3 ]
while 1 :
a = input ( "请输入: " )
if a. isdigit( ) and int ( a) in arry:
a = int ( a)
while a:
if a == 1 :
GetAllStudentById( )
SortMenu( )
break
if a == 2 :
GetAllStudentByGadeOrMoney( 3 )
SortMenu( )
break
if a == 3 :
GetAllStudentByGadeOrMoney( 8 )
SortMenu( )
break
elif a > 3 or a < 0 :
print ( "输入有误!" )
break
if a == 0 :
break
else :
print ( "请输入正确的选项0--3" )
def main ( ) :
arry = [ 0 , 1 , 2 , 3 , 4 , 5 ]
Menu(
)
while 1 :
a = input ( "请输入: " )
if a. isdigit( ) and int ( a) in arry:
a = int ( a)
while a:
if a == 1 :
PrintStudentList( )
Menu( )
break
if a == 2 :
AddStudent( )
Menu( )
break
if a == 3 :
ChangeStudent( )
Menu( )
break
if a == 4 :
DeleteStudent( )
Menu( )
break
if a == 5 :
SortData( )
Menu( )
break
elif a > 5 or a < 0 :
print ( "输入有误!" )
break
if a == 0 :
print ( "系统已退出!" )
exit( )
else :
print ( "请输入0--5!" )
main( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
539 540 541 542 543
文件:
注意:将表格excel文件放在代码相同目录下即可 ,否则应该在使用文件时填上绝对路径,否则会出现文件打不开,或者找不到等错误,在系统运行期间应该讲文件保存并关闭,否则当文件处于打开状态时无法进行修改,插入等操作,出现错误。
二、 采用文本文档保存数据实现的毕业生信息管理系统
基本思想与上述的相似,就不再这里阐述了,以下附上源码。
源码:
StudentInfo = [ '学号' , '姓名' , '性别' , '毕业年级' , '就业公司名称' , '电话号码(+86)' , '家庭住址' ]
def GetList ( ) :
fiel = open ( 'StudentMsg.txt' , 'r' , encoding= 'utf-8' )
l = [ ]
for line in fiel:
l. append( line. strip( ) )
return l
def PrintAllMsg ( ) :
l = GetList( )
print ( StudentInfo)
count = 0
for i in range ( 0 , len ( l) ) :
count = count + 1
print ( l[ i] , end= " " )
if count % 7 == 0 :
print ( )
print ( )
def ModifyMenu ( ) :
print ( '-' * 22 )
print ( "# 修改姓名请输入: 1 *" )
print ( "# 修改性别请输入: 2 *" )
print ( "# 修改毕业年级请输入: 3 *" )
print ( "# 修改公司信息请输入: 4 *" )
print ( "# 修改电话号码请输入: 5 *" )
print ( "# 修改家庭住址请输入: 6 *" )
print ( "# 退出修改请输入: 0 *" )
print ( '-' * 22 )
def ModifyMsg ( ) :
def ModifyName ( pos) :
f = open ( 'StudentMsg.txt' , 'r+' , encoding= 'utf-8' )
flist = f. readlines( )
name = input ( "输入修改之后的姓名:" )
name += '\n'
flist[ pos + 1 ] = name
f = open ( 'StudentMsg.txt' , 'w+' , encoding= 'utf-8' )
f. writelines( flist)
f. close( )
print ( "修改成功!" )
def ModifySex ( pos) :
Sex = [ '男' , '女' ]
f = open
( 'StudentMsg.txt' , 'r+' , encoding= 'utf-8' )
flist = f. readlines( )
sex = input ( "输入修改之后的性别:" )
if sex in Sex:
sex += '\n'
flist[ pos + 2 ] = sex
f = open ( 'StudentMsg.txt' , 'w+' , encoding= 'utf-8' )
f. writelines( flist)
f. close( )
print ( "修改成功!" )
else :
print ( "输入错误!" )
print ( "修改失败!" )
ModifySex( pos)
def ModifyYear ( pos) :
f = open ( 'StudentMsg.txt' , 'r+' , encoding= 'utf-8' )
flist = f. readlines( )
year = input ( "输入修改之后的年级:" )
if int ( year) > 2000 :
year += '\n'
flist[ pos + 3 ] = year
f = open ( 'StudentMsg.txt' , 'w+' , encoding= 'utf-8' )
f. writelines( flist)
f. close( )
print ( "修改成功!" )
else :
print ( "输入错误!" )
print ( "修改失败!" )
ModifyYear( pos)
def Modifycompany ( pos) :
f = open ( 'StudentMsg.txt' , 'r+' , encoding= 'utf-8' )
flist = f. readlines( )
company = input ( "输入修改之后的公司:" )
company += '\n'
flist[ pos + 4 ] = company
f = open ( 'StudentMsg.txt' , 'w+' , encoding= 'utf-8' )
f. writelines( flist)
f. close( )
print ( "修改成功!" )
def ModifyTell ( pos) :
f = open ( 'StudentMsg.txt' , 'r+' , encoding= 'utf-8' )
flist = f. readlines( )
tell = input ( "输入修改之后的电话号码:" )
if len ( tell) == 11 :
tell += '\n'
flist[ pos + 5 ] = tell
f = open ( 'StudentMsg.txt' , 'w+' , encoding= 'utf-8' )
f. writelines( flist)
f. close( )
print ( "修改成功!" )
else :
print ( "输入错误!" )
print ( "修改失败!" )
ModifyTell( pos)
def ModifyAddress ( pos) :
f = open ( 'StudentMsg.txt' , 'r+' , encoding= 'utf-8' )
flist = f. readlines( )
address = input ( "输入修改之后的地址:" )
address += '\n'
flist[ pos + 6 ] = address
f = open ( 'StudentMsg.txt' , 'w+' , encoding= 'utf-8' )
f. writelines( flist)
f. close( )
print ( "修改成功!" )
PrintAllMsg( )
id = input ( "请输入你要修改的学号:" )
if id in IsIdRight( ) :
l2 = GetList( )
pos = l2. index( id )
while 1 :
ModifyMenu( )
a = int ( input ( "请输入: " ) )
while a:
if a == 1 :
ModifyName( pos)
break
if a == 2 :
ModifySex( pos)
break
if a == 3 :
ModifyYear( pos)
break
if a == 4 :
Modifycompany( pos)
break
if a == 5 :
ModifyTell( pos)
break
if a == 6 :
ModifyAddress( pos)
break
if a == 0 :
break
def DelMsg ( ) :
PrintAllMsg( )
id = input ( "请输入你要删除的学生的Id:" )
if id in IsIdRight( ) :
pos = GetList( ) . index( id )
f = open ( 'StudentMsg.txt' , 'r+' , encoding= 'utf-8' )
flist = f. readlines( )
for i in range ( 0 , 7 ) :
del flist[ pos]
f = open ( 'StudentMsg.txt' , 'w+' , encoding= 'utf-8' )
f. writelines( flist)
f.
close( )
print ( "删除成功!" )
PrintAllMsg( )
else :
print ( "学号输入错误!" )
DelMsg( )
def IsIdRight ( ) :
l1 = GetList( )
l2 = [ ]
i = 0
while i < len ( l1) :
l2. append( l1[ i] )
i = i + 7
return l2
def AddMsg ( ) :
fiel = open ( 'StudentMsg.txt' , 'a' , encoding= 'utf-8' )
def Inputid ( ) :
id = ( input ( "请输入你的学号:" ) )
l1 = IsIdRight( )
if not ( int ( id ) > 1000 and ( id in l1) ) :
fiel. write( '\n' )
fiel. writelines( id )
else :
if int ( id ) < 1000 :
print ( "学号输入错误!" )
Inputid( )
if id in IsIdRight( ) :
print ( "学号存在!" )
Inputid( )
def Inputname ( ) :
name = input ( "请输入你的姓名:" )
fiel. write( '\n' )
fiel. writelines( name)
def InputSex ( ) :
sex = [ '男' , '女' ]
s1 = input ( "请输入你的性别" )
if s1 in sex:
fiel. write( '\n' )
fiel. writelines( s1)
else :
print ( "性别输入错误!" )
InputSex( )
def InputGaduYear ( ) :
year = ( input ( "请输入你的毕业年级:" ) )
if int ( year) > 2000 :
fiel. write( '\n' )
fiel. writelines( year)
else :
print ( "毕业年级输入错误!" )
InputGaduYear( )
def InputCompany ( ) :
company = input ( "请输入你的就业公司:" )
fiel. write(
'\n' )
fiel. writelines( company)
def InputTell ( ) :
tell = ( input ( "请输入你的电话号码:" ) )
if len ( tell) == 11 :
fiel. write( '\n' )
fiel. writelines( tell)
else :
print ( "电话号码输入错误!" )
InputTell( )
def InputAddress ( ) :
add = input ( "请输入你的家庭地址:" )
fiel. write( '\n' )
fiel. writelines( add)
Inputid( )
Inputname( )
InputSex( )
InputGaduYear( )
InputCompany( )
InputTell( )
InputAddress( )
fiel. close( )
def Menu ( ) :
print ( '-' * 22 )
print ( "# 查看毕业生列表输入: 1 *" )
print ( "# 添加毕业生信息输入: 2 *" )
print ( "# 修改毕业生信息输入: 3 *" )
print ( "# 查找毕业生信息输入:4 *" )
print ( "# 删除毕业生信息输入: 5 *" )
print ( "# 退出系统请输入 0 *" )
print ( '-' * 22 )
def FindMenu ( ) :
print ( '-' * 22 )
print ( "# 搜索学号请输入: 1 *" )
print ( "# 搜索姓名请输入: 2 *" )
print ( "# 退出搜所请输入 0 *" )
print ( '-' * 22 )
def FindStu ( ) :
def FindMsgById ( ) :
id = input ( "请输入你需要查找的学生的学号:" )
if id in IsIdRight( ) :
pos = GetList( ) . index( id )
flist = GetList( )
print ( StudentInfo)
for i in range ( 0 , 7 ) :
print ( flist[ pos + i] , end= " " )
print ( )
else :
print ( "学号输入错误!" )
FindMsgById( )
def FindMsgByName ( ) :
name = input ( "请输入你需要查找的学生的姓名:" )
if name in GetList( ) :
pos = GetList( ) .
index( name) - 1
flist = GetList( )
print ( StudentInfo)
for i in range ( 0 , 7 ) :
print ( flist[ pos + i] , end= " " )
print ( )
else :
print ( "姓名输入错误!" )
FindMsgByName( )
while 1 :
FindMenu( )
a = int ( input ( "请输入: " ) )
while a:
if a == 1 :
FindMsgById( )
break
if a == 2 :
FindMsgByName( )
break
if a == 0 :
break
def main ( ) :
Menu( )
while 1 :
a = int ( input ( "请输入: " ) )
while a:
if a == 1 :
PrintAllMsg( )
Menu( )
break
if a == 2 :
AddMsg( )
Menu( )
break
if a == 3 :
ModifyMsg( )
Menu( )
break
if a == 4 :
FindStu( )
Menu( )
break
if a == 5 :
DelMsg( )
Menu( )
break
if a == 0 :
exit( )
main( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
相应的简要测试:
相应的文件:注意 该文本文件应和代码文件在同一个目录下
注意 : 这里采用分行进行数据的存储,为了方便数据的准确修改,不需要一整行的数据进行修改那么麻烦,插入和修改也更为精确