首页 > 代码库 > python之修改用户配置文件
python之修改用户配置文件
用户的配置文件如下
backend oldboy
school school1 age 21 weight 210
qq 550124281 iphone 13925293887
backend oldgirl
school school2 age 22 weight 220
backend oldteacher
school school3 age 23 weight 230
backend oldstudent
school school4 age 24 weight 240
作业要求:
1、实现查询用户信息
2、实现增加用户信息
3、实现删除用户信息
name_list = [] tmp_list = [] info_list = [] f = open("homeworkdb","r",encoding="utf-8") for line in f: if line.startswith("backend"): tmp_list = (line.strip().split(" ")) # name_list.append(tmp_list[1]) # print(tmp_list) name_list.append(tmp_list[1]) f.close() flag = 0 # print(name_list) while True: option = int(input("1:查询用户信息\n2:删除用户信息\n3:增加用户信息\n4:退出\n")) if option == 1: name = input("please input name:") if name in name_list: with open("homeworkdb","r",encoding="utf-8") as f: for line in f: if flag == 0 and line.strip() == "backend " + name: flag = 1 continue if flag == 1 and not line.startswith("backend"): info_list.append(line.strip()) if flag == 1 and line.startswith("backend"): flag = 0 continue print("%s的信息是:" %(name)) for info in info_list: print(info) info_list = [] else: print("%s is not in list" %(name)) continue elif option == 2: name = input("please in name:") if name in name_list: with open("homeworkdb","r",encoding="utf-8") as f1,open("tmp","w",encoding="utf-8") as f2: for line in f1: if flag == 0 and line.strip() == "backend " + name: flag = 1 continue if flag == 1 and not line.startswith("backend"): continue if flag == 1 and line.startswith("backend"): flag = 0 if flag == 0: f2.write(line) with open("homeworkdb","w",encoding="utf-8") as f1,open("tmp","r",encoding="utf-8") as f2: for line in f2: f1.write(line) name_list.remove(name) with open("homeworkdb","r",encoding="utf-8") as f1,open("tmp","w",encoding="utf-8") as f2: for line in f1: if line == "\n": continue else: f2.write(line) with open("tmp","r",encoding="utf-8") as f1,open("homeworkdb","w",encoding="utf-8") as f2: for line in f1: f2.write(line) else: print("%s is not in list" %(name)) continue elif option == 3: name = input("please in name:") if name in name_list: print("%s is in list" % (name)) info = input("please input %s is information" % (name)) with open("homeworkdb" ,"r",encoding="utf-8") as f1: for line in f1: if flag == 0 and line.strip() == "backend " + name: flag = 1 continue if flag == 1 and not line.startswith("backend"): info_list.append(line) if flag == 1 and line.startswith("backend"): flag = 0 continue if info in tmp_list: print("%s is already exist" %(info )) else: with open("homeworkdb","r",encoding="utf-8") as f1,open("tmp","w",encoding="utf-8") as f2: flag = 0 for line in f1: # print(flag) if flag == 0 and line.strip() == "backend " + name: f2.write(line) tmp_info =" " + info + "\n" f2.write(tmp_info) else: f2.write(line) with open("tmp","r",encoding="utf-8") as f1,open("homeworkdb","w",encoding="utf-8") as f2: for line in f1: f2.write(line) else: print("%s is not in list" %(name)) info = input("please input %s is information" % (name)) with open("homeworkdb","a",encoding="utf-8") as f1: s1 = "\n" + "backend " + name s2 = "\n" + " " + info f1.write(s1) f1.write(s2) name_list.append(name) elif option == 4: exit() else: print("invalid input,please input[1|2|3|4]") continue
python之修改用户配置文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。