import os
filename='student.txt'
def main():
while True:
menu()
choice=int(input('请选择'))
if choice in [0,1,2,3,4,5,6,7]:
if choice==0:
answer=input('真的要退出系统吗?y/n')
if answer=='y' or answer=='Y':
print('谢谢使用')
break #退出系统
else:
continue
elif choice==1:
insert()
elif choice==2:
search()
elif choice==3:
delete()
elif choice==4:
modify()
elif choice==5:
sort()
elif choice==6:
total()
elif choice==7:
show()
def menu():
print('============================学生信息管理系统================================')
print('-----------------------------功能菜单-----------------------------------------')
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('------------------------------------------------------------------------------')
def insert():
student_list=[]
while True:
id=input('请输入ID')
if not id:
break
name=input('请输入姓名')
if not name:
break
try:
englist=int(input('请输入英语成绩'))
pylist=int(input('请输入python成绩'))
javalist=int(input('请输入Java成绩'))
except:
print('输入异常,请输入整数')
continue
#将录入的学生信息保存到字典中
student={'id':id,'name':name,'english':englist,'python':pylist,'java':javalist}
#将学生信息添加到列表中
student_list.append(student)
answer=input('是否继续添加y/n')
if answer=='y' or answer=='Y':
continue
else:
break
#将信息保存
save(student_list)
print('学生信息保存完毕')
def save(lst):
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 search():
student_query=[]
while True:
id=''
name=''
if os.path.exists(filename):
mode=input('按id查找请输入1,按名字查找请输入2')
if mode=='1':
id=input('请输入id')
elif mode=='2':
name=input('请输入名字')
else:
print('输入有误,请从新输入')
continue
with open(filename,'r',encoding='utf-8')as rfile:
student=rfile.readlines()
for item in student:
d=dict(eval(item))
if id!='':
if d['id']==id:
student_query.append(d)
elif name!='':
if d['name']==name:
student_query.append(d)
#显示查询结果
show_student(student_query)
#清空列表
student_query.clear()
answer=input('继续查询输入y/n\n')
if answer=='y'or'Y':
continue
else:
break
else:
print('暂未保存学生信息')
return
def show_student(lst):
if len(lst)==0:
print('没有查询到学生信息')
return
#定义标题显示格式
formate_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(formate_title.format('ID','姓名','英语成绩','python成绩','Java成绩','总成绩'))
#定义内容的显示格式
formate_date='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
for item in lst:
print(formate_date.format(item.get('id'),
item.get('name'),
item.get('englist'),
item.get('pylist'),
item.get('javalist'),
int(item.get('englist'))+int(item.get('pylist'))+int(item.get('javalist'))
))
def delete():
while True:
student_id=input('请输入要删除的id')
if student_id !='':
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8')as file:
student_old=file.readlines()
else:
student_old=[]
flag=False #标记是否删除
if student_old:
with open(filename,'w',encoding='utf-8')as wfile:
d={}
for item in student_old:
d=dict(eval(item)) #将字符串转为字典
if d['id'] !=student_id:
wfile.write(str(d)+'\n')
else:
flag=True
if flag:
print(f'id为{student_id}的学生信息已被删除')
else:
print(f'没有找到{student_id}的学生信息')
else:
print('无学生信息')
break
show() #删除之后重新显示学生信息
answer=input('是否继续删除y/n')
if answer=='y' or 'Y':
break
else:
break
这下面是报错:
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/20000块什么py课程/20000/studentsys/stusystem.py", line 191, in <module>
main()
File "C:/Users/Administrator/PycharmProjects/20000块什么py课程/20000/studentsys/stusystem.py", line 18, in main
search()
File "C:/Users/Administrator/PycharmProjects/20000块什么py课程/20000/studentsys/stusystem.py", line 98, in search
d=dict(eval(item))
ValueError: source code string cannot contain null bytes
Process finished with exit code 1
def search():
……
with open(filename,'r',encoding='utf-8')as rfile:
student=rfile.readlines()
for item in student:
d=dict(eval(item))
这里的item是遍历了student 但是你的student是从文件里readlines进来的 你没写非空判断
应该在这么写
with open(filename,'r',encoding='utf-8')as rfile:
student=rfile.readlines()
for item in student:
if item:
d=dict(eval(item))
else
print("列表为空")