zoukankan      html  css  js  c++  java
  • python作业之修改用户配置文件

    用户的配置文件如下

    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:查询用户信息
    2:删除用户信息
    3:增加用户信息
    4:退出
    "))
        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 == "
    ":
                            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 + "
    "
                                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 = "
    " + "backend " + name
                    s2 = "
    " + "     " + 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
    

      

     

      

  • 相关阅读:
    免费linux远程主机nitrousIO
    利用PyWapFetion发免费天气短信
    接口中的 sort排序 + md5加密
    即点即改心得
    js判断客户端访问是安卓还是ios
    php将时间戳转换成几小时前的格式封装
    前端请求接口出现的跨域问题
    使用redis技术实现注册登录列表以及关注功能
    windows下的redis安装以及扩展安装
    Yii2 学习心得
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/7041264.html
Copyright © 2011-2022 走看看