python 配置文件修改_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值