用户的配置文件如下

backend oldboy
school school1 age 21 weight 210
qq 550176565 iphone 139987676
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、实现删除用户信息

 

#Auther Bob
#--*--conding:utf-8 --*--
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.strip())
                    if flag == 1 and line.startswith("backend"):
                        flag = 0
                        continue
            # print(info_list)
            if info in info_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

  

 

  

posted on 2017-06-17 20:33  bainianminguo  阅读(595)  评论(0编辑  收藏  举报