700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python学生信息录入_使用python实现一个简单的学生信息管理系统

python学生信息录入_使用python实现一个简单的学生信息管理系统

时间:2020-09-08 16:21:40

相关推荐

python学生信息录入_使用python实现一个简单的学生信息管理系统

最近公司搬办公室,杂七杂八的事情比较多,又碰上业务要上线了。。。很多事情堆到一起来做,导致最近没什么时间学习,写博客。前两天胜利日放假,把以前用java写的学生信息管理系统用python重新写了一遍,以便于帮助python的学习。

好了,废话不多说,首先进行需求分析,下面是我根据需求画的系统结构图:

纯手工制图。。。。。画的不好敬请谅解。从上图来看,整个系统分为main,add,delete,change,select,sort,io,print共八个模块,实现了对学生信息的增删改查排的功能,将结果储存到student.txt文件中去。

学生信息的数据结构我将其设计为一个学生的一条记录用一个列表来存储,这个列表包含的信息为:学号,姓名,年龄,成绩,地址这些字段。同时,所有学生的记录又结合成一个列表,这样,这个列表就存储了所有学生的信息。

下面是我的源代码以及对该源代码的分析,以供大家借鉴参考以及自己的记录。(PS:由于本人学习Python的时间比较短,代码难免有写的比较渣的地方,希望各位大神轻喷(^-^!!!))catmain.py

#__author__='huxianglin'

#-*-coding:utf-8-*-

importadd

importdelete

importchange

importselect

importsort

file_path='student.txt'#首先定义数据的存储路径,这里定义为当前程序锁在目录的根目录中

defmain():#在main函数中使用while循环来获取用户的输入信息

whileTrue:

print(u"welcometostudentinformationmanagementsystem!")

print(u"youcanuseinput:add;delete;change;select;sort;quit")

keyword=raw_input("pleaseinputwhatyouwanttooperation:")

ifkeyword=="quit":#由于python中没有类似于switchcase的方式来实现多项选择,本来打算使用dict来实现这个功能的,但是按照网上的方式,死活无法达到想要的功能,于是,逼得没办法咬咬牙,用ifelif来代替switchcase的功能,请大神轻喷哈!

exit(0)

elifkeyword=="add":

add.index(file_path)

elifkeyword=="delete":

delete.index(file_path)

elifkeyword=="change":

change.index(file_path)

elifkeyword=="select":

select.index(file_path)

elifkeyword=="sort":

sort.index(file_path)

else:

print(u"pleaseinputcorrectoption!")

'''

else:#这一段实现有问题。。。无法达到目标。

option={"add":add.index(file_path),

"delete":delete.index(file_path),

"change":change.index(file_path),

"select":select.index(file_path),

"sort":sort.index(file_path)}

option.get(keyword,"yourinputiswrong,pleaseinputagain!")()

'''

if__name__=='__main__':

main()catadd.py

#__author__='huxianglin'

#-*-coding:utf-8-*-

importio

defindex(file_path):#这个函数是用来被main调用,实现添加记录的功能,通过这个函数来与用户交互,将获取到的数据通过io写入到文件中。

whileTrue:

print(u"pleaseinputastudentinformationlike:numbernameagescoreaddress")

print(u"Ifyouwanttobreak,pleaseinputquit!")

keyword=raw_input(u"pleaseinputwhatyouwant:")

ifkeyword=="quit":

break

else:

addline=add()

addline.addinfo(keyword.split(""),file_path)

classadd:#这里定义了一个类,主要用来实现将获取到的一条记录传到io中,写入文件。

def__init__(self):

print(u"wewillinputthisstudent'sinformation")

defaddinfo(self,student,file_path):

read=io.io()

read.add(student,file_path)catdelete.py

#__author__='huxianglin'

#-*-coding:utf-8-*-

importio

defindex(file_path):#同理,这里也是通过与用户交互,获取到用户想要删除的记录的学号,然后交给delete类来实现删除记录的功能。(ps:这里少了一个判断用户输入的信息是否被成功执行的功能,时间仓促,没有完善哈。)

whileTrue:

print(u"pleaseinputthatyouwanttodeletestudent'snumber!")

print(u"Ifyouwanttobreak,pleaseinputquit!")

keyword=raw_input(u"pleaseinputwhatyouwant:")

ifkeyword=="quit":

break

else:

deleteline=delete()

deleteline.deleteinfo(keyword,file_path)

classdelete:#这个类通过io获取到所有学生信息,然后将用户想要删除的学生学号进行匹配,假如匹配到,则删除该条记录。

defdeleteinfo(self,number,file_path):

read=io.io()

information=[]

information=read.read(information,file_path)

foriininformation:

ifi[0]==number:

information.remove(i)

read.write(information,file_path)catchange.py

#__author__='huxianglin'

#-*-coding:utf-8-*-

importio

defindex(file_path):#这里通过交互获取到用户想要修改的学生的学号,然后传递给change类。

whileTrue:

print(u"pleaseinputthatyouwanttochangestudent'snumber!")

print(u"Ifyouwanttobreak,pleaseinputquit!")

keyword=raw_input(u"pleaseinputwhatyouwant:")

ifkeyword=="quit":

break

else:

changeline=change()

changeline.changeinfo(keyword,file_path)

classchange:#这里通过从io中获取学生信息,存储到list中,然后遍历list,获取到匹配学号的记录,然后获取到用户想要修改后的数据,将其写入到list中,最后再将这个新的List存入到文件中(PS:这里卡了我好久,因为文件中的学生记录是一条记录存储一行,需要在记录的最后面添加上换行符,但是修改后的数据是没有换行符的,为了将换行符添加进去,我想了很多办法,最后才想到直接将输入的字符串和换行符进行链接,然后再存储到list中去。。。。。。表示真想狠狠抽自己嘴巴子。。。)

defchangeinfo(self,number,file_path):

read=io.io()

information=[]

information=read.read(information,file_path)

foriininformation:

ifi[0]==number:

print(u"pleaseinputthestudent'sinformationthatyouwanttochangelike:numbernameagescoreaddress")

keyword=raw_input(u"pleaseinputwhatyouwant:")

keyword=keyword+'\n'

information[information.index(i)]=keyword.split("")

read.write(information,file_path)catselect.py

#__author__='huxianglin'

#-*-coding:utf-8-*-

importio

importprintstudent

defindex(file_path):#通过交互,获取到用户想要查询的学生的学号,然后传入select类中进行查找。

whileTrue:

print(u"pleaseinputthatyouwanttoselectstudent'snumber!")

print(u"Ifyouwanttobreak,pleaseinputquit!")

keyword=raw_input(u"pleaseinputwhatyouwant:")

ifkeyword=="quit":

break

else:

selectline=select()

selectline.selectinfo(keyword,file_path)

classselect:#通过获取到用户想要查找的记录的学号,同时获取到学生信息列表,然后将其传入到printstudent块中进行处理。

defselectinfo(self,number,file_path):

read=io.io()

information=[]

information=read.read(information,file_path)

printstudent.selectstudent(number,information)catsort.py

#__author__='huxianglin'

#-*-coding:utf-8-*-

importio

importprintstudent

defindex(file_path):#同样的,通过交互获取到用户想要通过什么方式进行排序,可选择的有学号,年龄,成绩。

whileTrue:

print(u"pleaseinputwhatkeywordthatyouwanttosort,youcanchoose'number'or'age'or'score'thosekeywords")

print(u"Ifyouwanttobreak,pleaseinputquit!")

keyword=raw_input(u"pleaseinputwhatyouwant:")

ifkeyword=="quit":

break

elifkeyword=="number":

numbersort=sortstudent()

numbersort.numbersort(file_path)

elifkeyword=="age":

agesort=sortstudent()

agesort.agesort(file_path)

elifkeyword=="score":

scoresort=sortstudent()

scoresort.scoresort(file_path)

else:

print(u"pleaseinputcurrectkeyword")

classsortstudent:#通过index函数调用类中不同的排序关键字对应的方法,可以对学生信息通过关键字进行排序。(PS:我觉得这部分写的比较繁琐,可能可以进行更简化的操作,提高代码重用率,可是目前我没想到更好的办法。。。抱歉哈。。。)这里提到了sorted函数,这个函数可以对传入进去的列表进行排序,生成一个新的列表副本,同时里面还可以使用lambda对应列表嵌套进行排序,这个功能是一个很实用的功能,只想说好顶赞!!!

defnumbersort(self,file_path):

read=io.io()

information=[]

information=read.read(information,file_path)

sorted_information=sorted(information,key=lambdastudent:student[0])

printstudent.printstudent(sorted_information)

defagesort(self,file_path):

read=io.io()

information=[]

information=read.read(information,file_path)

sorted_information=sorted(information,key=lambdastudent:student[2])

printstudent.printstudent(sorted_information)

defscoresort(self,file_path):

read=io.io()

information=[]

information=read.read(information,file_path)

sorted_information=sorted(information,key=lambdastudent:student[3])

printstudent.printstudent(sorted_information)catprintstudent.py

#__author__='Administrator'

#-*-coding:utf-8-*-

defprintstudent(information):#这个没什么好说的,对查询和排序传过来的列表进行排序,其中,主要需要注意格式化输出。这里面使用了%+20s,其中的+20是占位20的宽度,数据向右对齐。由于排序和查询输出结构不一样,于是写了两个输出函数来实现(个人感觉这里也可以进行精简)。。。

foriininformation:

print("number:%+20s"%str(i[0]))

print("name:%+20s"%str(i[1]))

print("age:%+20s"%str(i[2]))

print("score:%+20s"%str(i[3]))

print("address:%+20s"%str(i[4]))

defselectstudent(num,information):

foriininformation:

ifi[0]==num:

print("number:%+20s"%str(i[0]))

print("name:%+20s"%str(i[1]))

print("age:%+20s"%str(i[2]))

print("score:%+20s"%str(i[3]))

print("address:%+20s"%str(i[4]))catio.py

#__author__='huxianglin'

#-*-coding:utf-8-*-

classio:#这部分是写的最纠结的,最开始由于对文件操作不熟悉,总是无法实现正确的换行写入操作。。。。。最开始选择逃避的办法,一条一条插入数据,后面发现写不下去了,还是需要写出写入文件的正确办法,在网上找了好久的资料终于在最后想到了在每个列表的末尾添加换行符来解决操作,于是,就有了上面那个修改数据后怎么添加换行符的操作。。。(PS:总觉得这种方式是拆东墙补西墙的感觉。。。碰到一个问题,解决一个,然后因为改动,又出现了新的问题。。。)这里一条一条插入数据使用了print的重定向功能,这样就自动在每条数据的最后面添加了换行符。

defread(self,information,file_path):

withopen(file_path,'r')asf:

forlineinf:

information.append(line.split(''))

returninformation

defwrite(self,information,file_path):

f=open(file_path,'w')

forlineininformation:

f.write(''.join(line))

f.close()

defadd(self,information,file_path):

f=open(file_path,'a')

print>>f,''.join(information)

f.close()catstudent.txt

127guojianlin2398shenzhen

123huxianglin2199shenzhen

126huxianglin2595shenzhen

124huxianglin2697shenzhen

125huxianglin2496shenzhen

129huxianglin2892shenzhen

128huxianglin2791shenzhen

122huxianglin2593shenzhen

131huxianglin2195shenzhen

上面是文件里面存储的信息,随便刷的。。。总结来看这次小的程序设计做的磕磕绊绊主要是由于对python不熟悉的原因,有很多想法就是不知道该如何实现。。。QAQ果然我还是练得太少了!!!

但是在这次重写过程中,我发现python确实开发速度比起java来要快不少,特别是处理文件类的操作,list,dict简直就是python的大杀器,以前我实现这个功能用java写的,差不多有500行代码才实现,用python实现这个功能才用了200行左右的代码就搞定了,这还是在我对python不怎么熟悉的基础上,由以上对比可以看出,pyhthon开发确实比java等这些语言开发速度快多了。这也给了我继续将python学下去的信心!!!学习的路上总会碰到沟沟坎坎,坚持下去,终能走到终点!加油,各位共勉之!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。