Python之小型信息管理系统
#Author:msq #Time:2019/11/16 import re import os filename = "person.txt" def menu(): #输出菜单 print(' '*5+'-'*15+"信息管理系统"+'-'*15) print(' '*5+'='*16+"功能菜单"+'='*16) print(''' 1 录入个人信息 2 查找个人信息 3 删除个人信息 4 修改个人信息 5 排序 6 统计总人数 7 显示所有信息 0 退出系统 ''') print(' '*5+'='*36) print(' '*5+"提示:通过数字选择菜单") print(' '*5+'-'*36) #1.录入个人信息 def insert(): personList = [] make =True while make: name = input("请输入姓名(如张三):") sex = input("请输入性别:") age = int(input("请输入年龄:")) id = input("请输入身份证号:") occupation = input("请输入职业(如学生):") person = {"name": name, "sex": sex, "age": age, "id": id, "occupation": occupation} personList.append(person) inputList = input("是否继续添加?(y/n):") if inputList == 'y': make = True else: make = False save(personList) print("信息录入完毕!!!") #保存信息到文件 def save(person): try: person_txt = open(filename, 'a')#以追加的模式打开 except Exception as e: person_txt = open(filename, 'w')#文件不存在就创建文件并打开 for info in person: person_txt.write(str(info)+"\n") #执行存储,添加换行符 person_txt.close() #2.查找个人信息 def search(): make =True person_query = [] while make: name = '' if os.path.exists(filename): mode = input("按照姓名查询输入1:按照身份证号查询输入2:") if mode == '1': name = input("请输入姓名(如张三):") elif mode == '2': id = input("请输入身份证号:") else : print("您的输入有误,请重新输入!") search() with open(filename, 'r') as file: person = file.readlines() for list in person: d = dict(eval(list)) if name is not "": if d['name'] == name: person_query.append(d) elif id is not "": if d["id"] == id: person_query.append(d) show_person(person_query) person_query.clear() inputMark = input("是否继续查询?(y/n):") if inputMark == 'y': make = True else: make = False else: print("暂未保存数据信息...") return #3.删除个人信息 def delete(): mark =True while mark: person_name = input("请输入要删除人的姓名:") if person_name is not "": if os.path.exists(filename): with open(filename,'r') as rfile: person_old = rfile.readlines() else: person_old = [] ifdel = False #标记是否删除 if person_old: #如果存在 with open(filename,'w') as wfile: d = {} #定义空字典 for list in person_old: d = dict(eval(list)) if d["name"] != person_name: #找到该条数据并替换成空字典 wfile.write(str(d)+"\n") else: ifdel = True #标记已经删除 if ifdel: print("name为%s个人信息已经被删除了..." % person_name) else: print("没有找到name为%s的个人信息" %person_name) else: print("不存在这个人的信息") break show() inputMark = input("是否继续删除?(y/n):") if inputMark == 'y': mark = True else: mark = False #4.修改个人信息 def modify(): show() #显示全部数据 if os.path.exists(filename): with open(filename,'r') as rfile: person_old = rfile.readlines() else: return person_name = input("请输入要修改的人的姓名:") with open(filename, 'w') as wfile: for person in person_old: d =dict(eval(person)) if d["name"] == person_name: print("找到了该条信息可以修改^_^") while True: #输入要修改的信息 d["name"] = input("请输入姓名:") d["sex"] = input("请输入性别") d["age"] = input("请输入年龄") d["id"] =input("请输入身份证号") d["occupation"] = input("请输入职业") else: break person = str(d) wfile.write(person+"\n") print("修改成功^_^") else: wfile.write(person) mark = input("是否继续修改其他信息?(y/n)") if mark == 'y': modify() #5.排序 def sort(): if os.path.exists(filename): with open(filename, 'r') as rfile: person_old = rfile.readlines() person_new = [] for list in person_old: d = dict(eval(list)) person_new.append(d) else: return asOrder = input("请选择(0升序;1降序):") if asOrder == '0': asOrderBool = False elif asOrder == '1': asOrderBool = True else: print("您输入的信息有误,请重新输入!") sort() mode = input("请输入排序方式(1按名字排序;2按身份证号排序):") if mode == '1': person_new.sort(key=lambda x: x["name"], reverse=asOrderBool) elif mode == '2': person_new.sort(key=lambda x: x["id"], reverse=asOrderBool) else: print("您输入的信息有误,请重新输入!") sort() show_person(person_new) #6.统计总人数 def total(): if os.path.exists(filename): with open(filename, 'r') as rfile: person_old = rfile.readlines() if person_old: print("一共有%d条数据"%len(person_old)) else: print("还没录入学生信息") else: print("暂未保存数据信息") #7.显示所有信息 def show(): person_new = [] if os.path.exists(filename): with open(filename, 'r') as rfile: person_old = rfile.readlines() for list in person_old: person_new.append(eval(list)) if person_new: show_person(person_new) else: print("暂未保存数据信息") #显示某个人信息 def show_person(personList): if not personList: print("无效的数据") return ''' format_title = "{:^5}\t{:^5}\t{:^5}\t{:^10}\t{:5}" print(format_title.format("名字", "性别", "年龄", "身份证号", "职业")) format_data = "{:^5}\t{:^5}\t{:^5}\t{:^10}\t{:5}" for info in personList: print(format_data.format((info.get("name"), info.get("sex"), info.get("age"), info.get("id"), info.get("occupation")))) ''' print("名字", " 性别", " 年龄", "身份证号", " 职业") for info in personList: print(info.get("name")+' ', info.get("sex")+' ', str(info.get("age"))+' ', info.get("id")+' ', info.get("occupation")) #0.退出系统 def main(): ctrl = True #标记是否退出系统 while(ctrl): menu() option = input("请选择:") option_str = re.sub("\D", "", option) #提取数字 if option_str in ['0','1','2','3','4','5','6','7']: option_int = int(option_str) if option_int == 0: print("您已经退出信息管理系统!") ctrl = False elif option_int == 1: insert() elif option_int == 2: search() elif option_int == 3: delete() elif option_int == 4: modify() elif option_int == 5: sort() elif option_int == 6: total() elif option_int == 7: show() if __name__=='__main__': main()