用户的配置文件如下

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

  

 

  

python作业之修改用户配置文件的更多相关文章

  1. python作业day3修改配置文件

    思维还有点乱,撸代码到深夜,先上代码吧.(我是跟着武sir的思路的) 流程图: 代码(有注释): #!/usr/bin/env python # -*- coding:utf-8 -*- import ...

  2. python读写增删修改ini配置文件

    一,百度百科 .ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的 ...

  3. xp默认安装路径及本地用户配置文件存放路径修改

    xp默认安装路径及本地用户配置文件存放路径修改 修改注册表可能会造成您的系统崩溃或数据丢失,请先行备份操作系统,以备不测!!! 备份注册表步骤: 1.在“桌面”上按快捷键“Ctrl+R”,调出“运行” ...

  4. python作业学员管理系统(第十二周)

    作业需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上课纪录对应多条学 ...

  5. python作业堡垒机(第十三周)

    作业需求: 1. 所有的用户操作日志要保留在数据库中 2. 每个用户登录堡垒机后,只需要选择具体要访问的设置,就连接上了,不需要再输入目标机器的访问密码 3. 允许用户对不同的目标设备有不同的访问权限 ...

  6. Python作业-选课系统

    目录 Python作业-选课系统 days6作业-选课系统: 1. 程序说明 2. 思路和程序限制 3. 选课系统程序目录结构 4. 测试帐户说明 5. 程序测试过程 title: Python作业- ...

  7. python作业ATM(第五周)

    作业需求: 额度 15000或自定义. 实现购物商城,买东西加入 购物车,调用信用卡接口结账. 可以提现,手续费5%. 支持多账户登录. 支持账户间转账. 记录每月日常消费流水. 提供还款接口. AT ...

  8. kafka实战教程(python操作kafka),kafka配置文件详解

    kafka实战教程(python操作kafka),kafka配置文件详解 应用往Kafka写数据的原因有很多:用户行为分析.日志存储.异步通信等.多样化的使用场景带来了多样化的需求:消息是否能丢失?是 ...

  9. 【HOW】用PowerShell脚本修改用户配置文件属性显示次序

    首先将如下脚本保存为PowerShell文件,如:ReorderUserProfileProperty.ps1. 在执行此脚本时,如果不输入任何参数,将列出所有用户配置文件属性的名称和显示次序:如果只 ...

随机推荐

  1. sersync基于rsync+inotify实现数据实时同步

    一.环境描述 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 主服务器A:192.168.1.23 从服务器B:192. ...

  2. [UE4]C++静态局部变量

    void testFunc() { ; // this only runs ONCE, even on // subsequent calls to testFunc()! cout << ...

  3. laravel 5.3 ——路由(资源,别名)

    laravel的路由定义中,其中route:resoure(),可以直接定义类似restful风格的URL 例如:Route::resource('system/role','System\RoleC ...

  4. Java基本类型与运算

    问题及答案来源自<Java程序员面试笔试宝典>第四章 Java基础知识 4.4基本类型与运算 1.Java提供了哪些基本数据类型? Java一共提高了八种原始的数据类型:byte.shor ...

  5. RouterOS 5.16软路由安装图解教程

    说明:RouterOS是一种路由器操作系统,它可以安装到普通的个人电脑上面,替代硬件路由器 RouterOS版本:RouterOS 5.16 硬件要求: 1.支持多核CPU 2.内存最大支持到2G 3 ...

  6. shell语法(二)

    Shell脚本语法 条件测试:test. [ ] 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1 ...

  7. Nginx-ingress-controller部署

    参考官网https://kubernetes.github.io/ingress-nginx/ 部署pod:nginx-ingress-controller/nginx-default-backend ...

  8. OpenACC数据管理语句

    ▶ 书中第4章,数据管理部分的代码和说明 ● 代码,关于 copy,copyin,copyout,create #include <stdio.h> #include <openac ...

  9. scala.XML处理

    XML scala提供了对xml字面量的内建支持,我们可以很容易的在程序代码中生成xml片段, scala类库也包含了对xml常用处理的支持 有时候scala会错误识别出xml字面量 如x < ...

  10. sssp maven pom

    pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.or ...