共计 4437 个字符,预计需要花费 12 分钟才能阅读完成。
#encoding : utf-8
#用户库 key 必须为自增 ----id
# users = {# 2 : {'name' : 'zhangsan', 'age' : 20, 'tel' : 12345},
# 5 : {'name' : 'wanger', 'age' : 23, 'tel' : 88888},
# 3 : {'name' : 'lisi', 'age' : 18, 'tel' : 45678},
# 1 : {'name' : 'xiaoh', 'age' : 23, 'tel' : 444444},
# 4 : {'name' : 'dadadad', 'age' : 23, 'tel' : 66666}
# }
users = {}
#打开 txt 文件,并保存到 users 字典中
user_file = open('/Users/macbookair/python/study/conf/user.txt','rt')
for user in user_file:
nodes = user.strip().split(',')
#文件中有空的换行会报错,所以加 if
iflen(nodes) != 1:
users[int(nodes[0])] = {'name' : nodes[1],'age' : int(nodes[2]),'tel' : int(nodes[3])}
user_file.close()
#定义表头的模版
tpl_title = '|{0:^10}|{1:^15}|{2:^10}|{3:^20}|'
colums_title = ('ID','Name','Age','Tel')
title = tpl_title.format(colums_title[0],colums_title[1],colums_title[2],colums_title[3],)
#定义分割线,跟随 title 长度变化
splitline = '-'*len(title)
#定义表数据模板
tpl_body = '|{0:^10}|{1:^15}|{2:^10}|{3:^20}|'
#判断用户登录是否正确
login_count = 0
isLogin = False
while login_count < 3:
login = input('请输入用户名, 联系方式登入 或 exit 退出登入:')
login_user = login.split(',')
iflen(login_user) == 2:
for key , value inlist(users.items()):
if login_user[1].isdigit():
if login_user[0] == value['name'] and int(login_user[1]) == value['tel']:
isLogin = True
break
if isLogin:
whileTrue:
tip = input('请输入操作命令(add/ 新增,del/ 删除,update/ 修改,query/ 查找,list/ 列表,exit/ 退回登入页)用户:')
if tip == 'add':
#以逗号分割字符串
user_add = input('请输入需要添加用户名,年龄,联系方式:')
nodes = user_add.split(',')
#判断字符是不是输入的三个
iflen(nodes) == 3:
if nodes[0].isdecimal():
print('用户名不能为全数字')
elif not nodes[1].isdigit():
print('请输入正确的年龄')
elif not nodes[2].isdigit():
print('请输入正确的联系方式')
else:
#输入的符合要求后,添加用户
#判断 users 不为空, 则 user_id = max(users) + 1
user_id = 1
if users:
user_id = max(users) + 1
users[user_id] = {'name' : nodes[0],'age' : nodes[1],'tel' : nodes[2]}
print('*** 用户:{0}, 添加成功!***'.format(nodes[0]))
else:
print('输入错误')
elif tip == 'del':
user_del = input('请输入需要删除的用户 ID/ 用户名:')
user_tf = False
#是否是数字
if user_del.isdigit():
user = users.pop(int(user_del),None)
if user:
print('*** 用户, 已删除!***')
else:
print('用户不存在')
else:
#name 是否存在
for key , value inlist(users.items()):
#转换成 int 判断,在不在用户 list 中
if user_del == value['name']:
del users[key]
print('*** 用户:{0}, 已删除!***'.format(value['name']))
user_tf = True
if not user_tf:
print('用户不存在')
elif tip == 'update':
user_update = input('请输入需要修改的用户 ID/ 用户名:')
user_tf = False
#判断输入的是不是 id,或者在不在 users 的 key 中
if user_update.isdecimal() and int(user_update) in users:
#判断这个用户在不在 users 中
new_user = input('请输入需要新的用户名,年龄,联系方式:')
nodes = new_user.split(',')
#判断字符是不是输入的三个
iflen(nodes) == 3:
if nodes[0].isdecimal():
print('用户名不能为全数字')
elif not nodes[1].isdecimal():
print('请输入正确的年龄')
elif not nodes[2].isdecimal():
print('请输入正确的联系方式')
else:
#字典可以根据 key 直接修改,如果 key 不存在则新增
users[int(user_update)] = {'name' : nodes[0],'age' : nodes[1],'tel' : nodes[2]}
print('*** 用户:{0}, 修改成功!***'.format(nodes[0]))
user_tf = True
else:
print('输入错误,请按照正确格式修改用户')
else:
#输入的用户名
for key, value inlist(users.items()):
#根据用户名查找用户
if user_update == value['name']:
new_user = input('请输入需要新的用户名,年龄,联系方式:')
nodes = new_user.split(',')
#判断字符是不是输入的三个
iflen(nodes) == 3:
if nodes[0].isdecimal():
print('用户名不能为全数字')
elif not nodes[1].isdecimal():
print('请输入正确的年龄')
elif not nodes[2].isdecimal():
print('请输入正确的联系方式')
else:
#字典可以根据 key 直接修改,如果 key 不存在则新增
users[key] = {'name' : nodes[0],'age' : nodes[1],'tel' : nodes[2]}
print('*** 用户:{0}, 修改成功!***'.format(nodes[0]))
user_tf = True
break
if not user_tf:
print('该用户不存在')
elif tip == 'query':
user_query = input('请输入需要查询的用户 ID/ 用户名:')
user_tf = False
#判断输入的字符串中是不是都是数字
if user_query.isdecimal():
#遍历用户 list
for key , value in users.items():
#转换成 int 判断,在不在用户 list 中
ifint(user_query) == key:
body = tpl_body.format(key,value['name'],value['age'],value['tel'])
#根据用户 ID,打印这个用户的信息
print(splitline)
print(title)
print(splitline)
print(body)
print(splitline)
user_tf = True
if not user_tf:
print('用户不存在')
else:
for key, value in users.items():
#转换成 int 判断,在不在用户 list 中
# 模糊查询 if user_query == value['name']:
if user_query == value['name']:
body = tpl_body.format(key,value['name'],value['age'],value['tel'])
#根据用户 ID,打印这个用户的信息
print(splitline)
print(title)
print(splitline)
print(body)
print(splitline)
user_tf = True
if not user_tf:
print('用户不存在')
elif tip == 'list':
#打印 分割线和表头字段名
print(splitline)
print(title)
print(splitline)
#转换成 list,并且排序
user_list = list(users.items())
user_list.sort()
#循环打印用户信息
for i in user_list:
user_value = i[1]
body = tpl_body.format(i[0],user_value['name'],user_value['age'],user_value['tel'])
print(body)
#打印最后的分割线
print(splitline)
elif tip == 'exit':
user_file2 = open('/Users/macbookair/python/study/conf/user.txt','wt')
for key , value in users.items():
user = '{0},{1},{2},{3}\n'.format(str(key),value['name'],value['age'],value['tel'])
user_file2.write(user)
user_file2.flush()
user_file2.close()
print('退出成功!')
break
else:
print('请输入正确的操作命令')
else:
print('登入失败')
login_count += 1
elif login == 'exit':
print('退出登入成功')
break
else:
print('请输入正确格式')
login_count += 1
正文完