python学习之-成员信息增删改查
python学习之-成员信息增删改查
主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,
在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证, #!/usr/bin/env python
# coding=utf8
#author:shantuwqk@163.com import os, sys, time,json
import re member_dict = \
{}
member_name_list = []
member_list = []
def handler_member_storage(name,passwd,mobile,email): member_detailed = {}
member_detailed[‘name‘] = name
member_detailed[‘passwd‘] = passwd
member_detailed[‘mobile‘] = mobile
member_detailed[‘email‘] = email
member_list.append(member_detailed)
member_name_list.append(name)
member_dict[‘namelist‘] = member_name_list
member_dict[‘namedetail‘] = member_list
#with open(‘member_info.json‘,‘w‘) as f:
# json.dump(member_dict,f)
return member_dict def handel_query_user(x):
"""
判断x如果是str,查询name字段,如果是int,查询手机号字段
:param x:
:return:
"""
#print "字符串",isinstance(x,str)
#print "数字",isinstance(x,int)
if isinstance(x,str):
if x in load_member_data()[‘namelist‘]:
print "精确匹配查询用户[%s]信息....."%(x)
print "==================================================="
user_index = load_member_data()[‘namelist‘].index(x)
user_detail = load_member_data()[‘namedetail‘][user_index]
for k,v in user_detail.items():
print k,v
print "----------------------------------------------------"
else:
print "\033[33;1m用户名或密码不在本数据库,你是不是记错了?您再想想!!\033[0m" if isinstance(x,int):
if check_mobileORmail_member(str(x)) != "mobile_true":
print "这是手机号吗,打打试试...."
else:
print "精确匹配查询用户手机[%s]信息......"%(x)
print "===================================================="
user_detail = load_member_data()[‘namedetail‘]
for user_dict in user_detail:
if x == int(user_dict[‘mobile‘]):
for k,v in user_dict.items():
print k,v
print "------------------------------------------------" def load_member_data():
with open("member_info.json",‘r‘) as f:
try:
member_info_dict = json.load(f)
return member_info_dict
except ValueError:
#print "\033[33;1m没有数据可加载\033[0m"
return False def check_login(user,passwd):
"""
验证登录用户名和密码是否正确
:param user:
:param passwd:
:return:
"""
with open("member_info.json",‘r‘) as f:
member_info_dict = json.load(f)
#print member_info_dict
if user in member_info_dict[‘namelist‘]:
user_index = member_info_dict[‘namelist‘].index(user)
user_detail = member_info_dict[‘namedetail‘][user_index]
#print user_index,user_detail
if passwd == user_detail[‘passwd‘] and user == user_detail[‘name‘]:
print "\033[32;1m输入用户[%s]密码[%s]正确\033[0m"%(user,passwd)
return True
else:
print "\033[31;1m 您输入的密码不正确\033[0m"
else:
print "\033[31;1m你查询的用户不存在\033[0m" def check_mobileORmail_member(x):
p=re.compile(r‘^[\w\d]+[\d\w\_\.]+@([\d\w]+)\.([\d\w]+)(?:\.[\d\w]+)?$|^(?:\+86)?(\d{3})\d{8}$|^(?:\+86)?(0\d{2,3})\d{7,8}$‘)
m = p.match(x)
if m == None:
print "\033[33;1mmail or mobile number is wrong!!\033[0m"
return False
else:
if m.group(1)!=None:
if m.group(1) == ‘vip‘:
print ‘It is %s mail,the address is %s‘ %(m.group(2),m.group())
return "mail_true"
else:
print ‘It is %s mail,the address is %s‘ %(m.group(1),m.group())
return "mail_true"
else:
if m.group(3)!=None:
print ‘It is mobilephone number,the number is %s‘ %m.group()
return "mobile_true"
else:
print ‘It is telephone number,the number is %s‘ %m.group()
return True class Modify_User_Info:
"""
修改用户的相关信息
"""
def update_passwd(self,user,newpwd):
if user in load_member_data()[‘namelist‘]:
user_index = load_member_data()[‘namelist‘].index(user)
user_detail = load_member_data()[‘namedetail‘][user_index]
user_detail[‘passwd‘] = newpwd
newpwd_dict = load_member_data()
newpwd_dict[‘namedetail‘][user_index] = user_detail
return newpwd_dict
else:
print "你有没有搞错!你输入的用户不在数据库" def update_mobile(self,user,newmobile):
if user in load_member_data()[‘namelist‘]:
user_index = load_member_data()[‘namelist‘].index(user)
user_detail = load_member_data()[‘namedetail‘][user_index]
user_detail[‘mobile‘] = newmobile
newpwd_dict = load_member_data()
newpwd_dict[‘namedetail‘][user_index] = user_detail
return newpwd_dict
else:
print "你有没有搞错!你输入的用户不在数据库" def update_email(self,user,newmail):
if user in load_member_data()[‘namelist‘]:
user_index = load_member_data()[‘namelist‘].index(user)
user_detail = load_member_data()[‘namedetail‘][user_index]
user_detail[‘email‘] = newmail
newpwd_dict = load_member_data()
newpwd_dict[‘namedetail‘][user_index] = user_detail
return newpwd_dict
else:
print "你有没有搞错!你输入的用户不在数据库" def del_user_info(user):
if user in load_member_data()[‘namelist‘]:
user_index = load_member_data()[‘namelist‘].index(user)
user_detail = load_member_data()[‘namedetail‘][user_index]
new_del_dict = load_member_data()
new_del_dict[‘namedetail‘].remove(user_detail)
new_del_dict[‘namelist‘].remove(user)
return new_del_dict else:
print "你要删除的用户[%s]不在数据库里"%user if __name__ == "__main__":
Operation_type = {
‘1‘:‘Add user information‘,
‘1.1‘:‘name,passwd,mobile,email‘,
‘2‘:‘Query the user information‘,
‘3‘:‘Modify the user information‘,
‘4‘:‘Delete user information‘,
‘5‘:‘logout‘
} while True:
print """
\033[32;1m********************************************************************************
Welcome to login user information center, you can make the following operation:
1,%s
2,%s
3,%s
4,%s
5,%s
What you need to operate?
********************************************************************************\033[0m
"""%(Operation_type[‘1‘],Operation_type[‘2‘],Operation_type[‘3‘],Operation_type[‘4‘],Operation_type[‘5‘])
option = raw_input("Choice is?>>") if option == ‘1‘:
print "\033[32;1m you have choice is [%s]\033[0m"%Operation_type[‘1‘]
print "You need to complete these input according to [%s] the guidelines"%Operation_type[‘1.1‘]
while True:
name = raw_input("name:")
passwd = raw_input("passwd:")
mobile = raw_input("mobile:")
email = raw_input("email:")
if len(name) == 0 or len(passwd) == 0 or len(mobile) == 0 or len(email) == 0:
print "\033[33;1mThe user information you entered any item can‘t be empty or incorrect.Please fill in again!!\033[0m"
continue
if check_mobileORmail_member(mobile) != "mobile_true" or check_mobileORmail_member(email) != "mail_true":
continue #print "#########3",name,passwd,mobile,email print """\033[32;1m 您输入的信息如下:
name:%s
passwd:%s
mobile:%s
email:%s
\033[0m"""%(name,passwd,mobile,email)
s1 = raw_input("y 保存/w 重新填写>>")
if s1 == "y": old_user_dict = load_member_data()
new_user_dict = handler_member_storage(name,passwd,mobile,email)
#print "############33",old_user_dict,‘\n‘,new_user_dict
#合并旧的和新的用户数据
if old_user_dict == False:
print "没有旧数据可加载,直接写入新数据"
with open(‘member_info.json‘,‘w‘) as f:
json.dump(new_user_dict,f)
s11 = raw_input("t 继续添加/b 返回上一级/q退出操作?>>")
if s11 == "t":
continue
elif s11 == "b":
break
elif s11 == "q":
sys.exit(0)
else:
for user in new_user_dict[‘namelist‘]:
old_user_dict[‘namelist‘].append(user)
for user_detail in new_user_dict[‘namedetail‘]:
old_user_dict[‘namedetail‘].append(user_detail)
#print old_user_dict,
with open(‘member_info.json‘,‘w‘) as f:
json.dump(old_user_dict,f)
print "保存成功"
s12 = raw_input("t 继续添加/b 返回上一级/q退出操作?>>")
if s12 == "t":
continue
elif s12 == "b":
break
elif s12 == "q":
sys.exit(0)
elif s1 == "w":
continue
else:
pass elif option == ‘2‘:
print "\033[32;1m you have choice is [%s]\033[0m"%Operation_type[‘2‘]
print "\033[33;1m友情提示:输入正确的用户名和密码才能查询用户信息\033[0m"
luser = raw_input("用户名:").strip()
lpasswd = raw_input("用户密码:").strip()
if check_login(luser,lpasswd) == True:
print "用户验证通过"
print "请输入需要查询的用户名或手机号[目前只支持精确查询]"
while True:
query_info = raw_input("查询:>>").strip()
if check_mobileORmail_member(query_info) == "mobile_true":
handel_query_user(int(query_info))
else:
handel_query_user(query_info)
s2 = raw_input("b 返回上一级/t 继续查询")
if s2 == "b":
break
elif s2 == "t":
continue else:
print "用户名或密码不正确,不能查询其它人信息" elif option == ‘3‘:
print "\033[32;1m you have choice is [%s]\033[0m"%Operation_type[‘3‘]
print "友情提示:目前支持修改用户的密码,手机号,邮箱地址"
modify = Modify_User_Info()
modify_option = raw_input("p 修改密码/m 修改手机号/e 修改邮箱地址")
if modify_option == "p":
user_name = raw_input("用户名:").strip()
user_pwd = raw_input("新密码:").strip()
print "开始修改用户[%s]新的密码为[%s]....."%(user_name,user_pwd)
update_dict = modify.update_passwd(user_name,user_pwd)
with open(‘member_info.json‘,‘w‘) as f:
json.dump(update_dict,f)
print "密码修改成功!"
elif modify_option == "m":
user_name = raw_input("用户名:").strip()
user_mobile = raw_input("新手机号:").strip()
print "开始修改用户[%s]新的手机号为[%s]....."%(user_name,user_mobile)
update_dict = modify.update_mobile(user_name,user_mobile)
with open(‘member_info.json‘,‘w‘) as f:
json.dump(update_dict,f)
print "手机号修改成功!"
elif modify_option == "e":
user_name = raw_input("用户名:").strip()
user_mail = raw_input("新邮箱:").strip()
print "开始修改用户[%s]新的邮箱为[%s]....."%(user_name,user_mail)
update_dict = modify.update_email(user_name,user_mail)
with open(‘member_info.json‘,‘w‘) as f:
json.dump(update_dict,f)
print "邮箱修改成功!" elif option == ‘4‘:
print "\033[32;1m you have choice is [%s]\033[0m"%Operation_type[‘4‘]
print "友情提示:请输入用户名"
user_name = raw_input("用户名:")
new_del_dict = del_user_info(user_name)
with open(‘member_info.json‘,‘w‘) as f:
json.dump(new_del_dict,f)
print "用户[%s]删除成功!!"%(user_name)
elif option == ‘5‘:
print "\033[32;1m you have choice is [%s]\033[0m"%Operation_type[‘5‘]
sys.exit(0)
else:
print "\033[31;1m The choice of the invalid \033[0m" #print check_mobileORmail_member(‘123456‘)
#print check_login(‘testwqk‘,‘testwqk‘)
#print handel_query_user(18600404875)
python学习之-成员信息增删改查的更多相关文章
- python学习之成员信息增删改查
主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env python# coding=utf8# ...
- python 学习分享-实战篇增删改查作业
一大波函数来袭 作业要求: 1本次作业通过空格及逗号,将文件拆分成列表,在通过判断add.del.update.select等关键字,来判断用户执行的是哪种命令,根据不同的命令调用不同的函数去处理. ...
- Python学习笔记-列表的增删改查
- python全栈开发中级班全程笔记(第二模块、第三章)(员工信息增删改查作业讲解)
python全栈开发中级班全程笔记 第三章:员工信息增删改查作业代码 作业要求: 员工增删改查表用代码实现一个简单的员工信息增删改查表需求: 1.支持模糊查询,(1.find name ,age fo ...
- EF学习笔记-1 EF增删改查
首次接触Entity FrameWork,就感觉非常棒.它节省了我们以前写SQL语句的过程,同时也让我们更加的理解面向对象的编程思想.最近学习了EF的增删改查的过程,下面给大家分享使用EF对增删改查时 ...
- python manage.py shell 的增删改查
python manage.py shell 的增删改查 guoguo-MacBook-Pro:myblog guoguo$ python manage.py shell Python 3.5.1 ( ...
- python基础学习之类的属性 增删改查
类中的属性如何在类外部使用代码进行增删改查呢 增加.改变: setattr内置函数以及 __setattr__魔法方法 class A: aaa = '疏楼龙宿' a = A() setattr(a, ...
- python 10min系列之实现增删改查系统
woniu-cmdb 奇技淫巧--写配置文件生成增删改查系统 视频教程 项目主页跪求github给个star, 线上demo,此页面都是一个配置文件自动生成的 详细的文章介绍和实现原理分析会发布在我的 ...
- 记一些Python(Pymysql)建表、增删改查等基础操作(小白适用)
1.读取sql文件创建数据表 有一个形如下图的sql文件,使用python读取文件并在数据库中创建所有的表. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道 ...
随机推荐
- python读写Excel文件的函数--使用xlrd/xlwt
python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket The ...
- 使用appium做自动化时如何切换activity
在使用appium过程中遇到了执行一个用例时有多个不同的acitivity的情况,以下为app内部切换acitivity的方法: 如果仅需要切换一次activity,可以通过设置desired_cap ...
- refreshcontrol 实现下拉刷新的功能
该组件实现下拉刷新的功能.不过该组件是用在ScrollView的内部的,为ScrollView添加一个下拉刷新的功能.当ScrollView的垂直方向的偏移量scrollY:0的时候,手指往下拖拽Sc ...
- 一种计算e的方法
原文地址:http://hankjin.blog.163.com/blog/static/3373193720108811316123/ 原理:平均e个(0,1)之间的随机数之和会大于1.原因:n个数 ...
- Sublime text 2 快捷键配置文件
分屏快捷键 command+alt+2(就是view菜单中layout后跟数字,有1234,快捷键都带有提示符) 格式化快捷键 ctrl+alt+f 这里提到一个和sublime无关的..comman ...
- IE8一枝独秀的JS兼容BUG
// 例如淡入淡出的封装类文件 function ImagesEff(div,time){ this.arr=[];//装载所有div this.time=time; this.recordOld=n ...
- yum仅下载RPM包不安装
http://www.ttlsa.com/linux/howto-yum-download-rpm-without-install/
- [置顶] export命令-linux
export 命令 功能说明: 设置或显示环境变量. 语 法: export [-fnp][变量名称]=[变量设置值] 补充说明: 在shell中执行程序时,shell会提供一组环境变量. expor ...
- 常用的用户状态命令包括:whoami、id、groups、newgrp 等
用户状态命令 常用的用户状态命令包括:whoami.id.groups.newgrp 等.
- 数据结构(Splay平衡树): [NOI2007] 项链工厂
[NOI2007] 项链工厂 ★★★ 输入文件:necklace.in 输出文件:necklace.out 简单对比 时间限制:4 s 内存限制:512 MB [问题描述] T公司是一 ...