1 #-*- coding:utf-8 -*- 2 3 filename = "students.txt" 4 import os 5 6 def save(student): 7 try: 8 students_txt = open(filename,"a") 9 except Exception as e: 10 students_txt = open(filename,"w") 11 12 for info in student: 13 students_txt.write(str(info)+"\n") 14 students_txt.close() 15 16 def insert(): 17 studentList = [] 18 mark = True 19 while mark: 20 id = input("请输入id(如1001):") 21 if not id: 22 break 23 #查找该学号是否存在 24 with open(filename,"r") as rfile: 25 student_old = rfile.readlines() 26 27 for student in student_old: 28 item = dict(eval(student)) 29 if item["id"] == id: 30 print("该学生已存在,请使用其他学号!!") 31 insert() 32 name = input("请输入名字:") 33 if not name: 34 break 35 try: 36 english = int(input("请输入英语成绩:")) 37 python = int(input("请输入Python成绩:")) 38 c = int(input("请输入C语言成绩")) 39 except: 40 print("输入无效,不是整形数据............重新录入信息") 41 continue 42 student = {"id":id,"name":name,"english":english,"python":python,"c":c} 43 studentList.append(student) 44 inputMark = input("是否继续添加?(Y/N):") 45 if inputMark == "y": 46 mark = True 47 else: 48 mark = False 49 save(studentList) 50 print("学生信息录入完毕!!") 51 52 def delete(): 53 mark = True 54 while mark: 55 studentId = input("请输入想要删除的学生ID:") 56 if studentId is not "": 57 if os.path.exists(filename): 58 with open(filename,"r") as rfile: 59 student_old = rfile.readlines() 60 else: 61 student_old = [] 62 ifdel = False 63 if student_old: 64 with open(filename,"w") as wfile: 65 d = {} 66 for list in student_old: 67 d = dict(eval(list)) 68 if d["id"] != studentId: 69 wfile.write(str(d)+"\n") 70 else: 71 ifdel = True 72 if ifdel: 73 print("ID为 %s 的学生信息已经被删除..........."%studentId) 74 else: 75 print("没有找到ID为 %s 的学生信息..."%studentId) 76 else: 77 print("无此学生信息") 78 break 79 show() 80 inputMark = input("是否继续删除?(y/n):") 81 if inputMark == "y": 82 mark = True 83 else: 84 mark = False 85 86 def modify(): 87 show() 88 if os.path.exists(filename): 89 with open(filename,"r") as rfile: 90 student_old = rfile.readline() 91 else: 92 return 93 94 studentId = input("请输出要修改的学生ID:") 95 with open(filename,"w") as wfile: 96 for student in student_old: 97 d = dict(eval(student)) 98 if d["id"] == studentId: 99 print("找到了这名学生,可以修改他的信息!") 100 while True: 101 try: 102 d["name"] = input("请输入姓名:") 103 d["english"] = int(input("请输入英语成绩: ")) 104 d["python"] = int(input("请输入Python成绩:")) 105 d["c"] = int(input("请输入C语言成绩: ")) 106 except: 107 print("您输入有误,请重新输入。") 108 else: 109 break 110 student = str(d) 111 wfile.write(student+"\n") #将修改后的学生信息写入文件 112 print("修改成功!") 113 else: 114 wfile.write(student) #将未修改的学生信息写入文件 115 116 mark = input("是否继续修改其他学生信息?(y/n)") 117 if mark == True: 118 modify() #使用回调函数 119 120 def search(): 121 mark = True 122 student_query = [] 123 while mark: 124 id = "" 125 name = "" 126 if os.path.exists(filename): 127 mode = input("按ID查找输入1;按学生姓名查找输入2: ") 128 if mode == "1": 129 id = input("请输入学生ID:") 130 elif mode == "2": 131 name = input("请输入学生名字: ") 132 else: 133 print("您输入的查找信息有误,请重新输入!!") 134 search() 135 with open(filename,"r") as rflie: 136 student = rflie.readlines() 137 for list in student: 138 d = dict(eval(list)) 139 if id is not "": 140 if d["id"] == id: 141 student_query.append(d) 142 elif name is not "": 143 if d["name"] == name: 144 student_query.append(d) 145 show_student(student_query) 146 student_query.clear() 147 inputMark = input("是否继续查询? (y/n):") 148 if inputMark == "y": 149 mark = True 150 else: 151 mark = False 152 else: 153 print("暂未保存数据信息。。。") 154 return 155 156 def show_student(student): 157 if not student: 158 print("无数据信息。。。。。。。。") 159 return 160 format_title = "{:^6}{:^12}\t{:^8}\t{:^26}\t{:^10}\t{:^20}" 161 print(format_title.format("ID","名字","英语成绩","Python成绩","C语言成绩","总成绩")) 162 format_data = "{:^6}{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}" 163 for info in student: 164 print(format_data.format(info.get("id"),info.get("name"),str(info.get("english")),\ 165 str(info.get("python")),str(info.get("c")),\ 166 str(info.get("english")+info.get("python")+info.get("c")).center(12))) 167 168 def total(): 169 if os.path.exists(filename): 170 with open(filename,"r") as rfile: 171 student_old = rfile.readlines() 172 if student_old: 173 print("一共有 %s 名学生"%len(student_old)) 174 else: 175 print("还没有录入学生信息") 176 else: 177 print("暂未保存数据信息。。") 178 179 def show(): 180 student_new = [] 181 if os.path.exists(filename): 182 with open(filename,"r") as rfile: 183 student_old = rfile.readlines() 184 185 for list in student_old: 186 student_new.append(eval(list)) 187 if student_new: 188 show_student(student_new) 189 else: 190 print("暂未保存数据信息......") 191 192 def sort(): 193 show() 194 if os.path.exists(filename): 195 with open(filename,"r") as rfile: 196 student_old = rfile.readlines() 197 student_new = [] 198 for list in student_old: 199 d = dict(eval(list)) 200 student_new.append(d) 201 else: 202 return 203 204 ascORdesc = input("请选择(0升序;1降序): ") 205 if ascORdesc == "0": 206 ascORdescBool = False 207 elif ascORdesc == "1": 208 ascORdescBool = True 209 else: 210 print("您输入有误请重新输入!") 211 sort() 212 213 mode = input("请选择排列方式(1按英语成绩排序;2按Python成绩排序;3按C语言成绩排序;0按总成绩排序): ") 214 if mode == "1": 215 student_new.sort(key=lambda x:x["english"],reverse=ascORdescBool) 216 217 elif mode == "2": 218 student_new.sort(key=lambda x:x["python"],reverse=ascORdescBool) 219 elif mode == "3": 220 student_new.sort(key=lambda x:x["c"],reverse=ascORdescBool) 221 elif mode == "0": 222 student_new.sort(key=lambda x:x["english"]+x["python"]+x["c"],reverse=ascORdescBool) 223 else: 224 print("您输入有误请重新输入!") 225 sort() 226 show_student(student_new) 227 228 229 230 231 232 233 234 235 236 237 238 239