python练习题-员⼯信息表
周末⼤作业:实现员⼯信息表
⽂件存储格式如下:
id,name,age,phone,job
1,Alex,22,136********,IT
2,Egon,23,133********,Tearcher
3,nezha,25,1333235322,IT
现在需要对这个员⼯信息⽂件进⾏增删改查。
不允许⼀次性将⽂件中的⾏都读⼊内存。
基础必做:
a.可以进⾏查询,⽀持三种语法:
select 列名1,列名2,… where 列名条件
⽀持:⼤于⼩于等于,还要⽀持模糊查。
⽰例:
select name, age where age>22
select * where job=IT
select * where phone like 133
进阶选做:
b.可创建新员⼯记录,id要顺序增加
c.可删除指定员⼯记录,直接输⼊员⼯id即可
d.修改员⼯信息
语法:set 列名=“新的值” where 条件
#先⽤where查对应⼈的信息,再使⽤set来修改列名对应的值为“新的值”
注意:要想操作员⼯信息表,必须先登录,登陆认证需要⽤装饰器完成
其他需求尽量⽤函数实现
import os
name_list = ['id', 'name', 'age', 'phone', 'job'] #创建列名列表
触发器sql语句ditons=['>','<','=','like']
user_dic={'name':'admin','passwd':'123456'}
status={'name':None,'passwd':False,'login':False}
def auth(x):
def user_auth(*args,**kwargs):
if not status['login']:#获取user_dic字典⾥⾯的login值
username=input('请输⼊⽤户名')
userpasswd=input('请输⼊密码')
if username==user_dic['name'] and userpasswd==user_dic['passwd']:#判断账户密码是否正确
status['name']=username #如果正确修改user_dic⾥⾯的信息
status['passwd']=userpasswd
status['login']=True
print('登陆成功')
res=x(*args,**kwargs)
return res
else:
print('⽤户名或密码输⼊错误')
else:#如果login为True,直接执⾏函数
res=x(*args,**kwargs)
return res
return user_auth#函数闭包
def check():send的过去分词
s=input('请输⼊查询语句,例如 select * where age=25\n')
if'select'and'where'in s:#检查输⼊语句是否正确
content, condition = s.split('where') #以where分割s,并且赋值给content,condition
content = ''.join(content.split('select')[1:]) #content 以select分割,取最后⼀个赋值给content
if content.strip() and condition.strip(): #判断如果content 和condition都不为空,执⾏下⾯语句
for key in ditons: #for循环遍历ditions,
if key in condition: #如果key 在condition⾥⾯的话,执⾏下列语句
index_condition = ditons.index(key)#获取key在ditons⾥⾯的索引
name, cond = condition.strip().split(key)#以key分割condition,赋值给name,cond
if name in name_list:#如果name在name_list⾥⾯,执⾏下列语句
with open('mysql', encoding='utf-8') as f: # r模式打开员⼯信息表
for line in f: # 逐⾏循环
if line.strip(): # 如果这⼀⾏不为空
line_list = line.strip().replace(',', ',').split(',')#将line以逗号分割以列表形式赋值给line_list,
if key == ">"and int(line_list[name_list.index(name)]) > int(cond): #如果key为> 且列表对应索引的值⼤于查询语句的值,那么就执⾏下⾯语句
if content.strip() == '*':#如果content为*,也就是全部打印
print(line.strip()) #打印line
else:
if','in content:#如果逗号在content⾥⾯,意味着打印多个,不打印全部
str1 = ''#定义⼀个空字符串
select_name_list = content.strip().split(',') #content 以逗号分割,以列表的形式赋值给select_name_list
select_name_list = [i for i in select_name_list if i != ''] #去掉select_name_list 的空字符串
for names in select_name_list:#for 循环select_name_list
names_index = name_list.index(names.strip())#到关键字的索引
str1 = str1 + line_list[names_index] + ','#赋值给str1
sql server备份表的sqlprint(str1)#打印
else:
print(line_list[name_list.index(content.strip())])#如果不存在逗号,只打印单个,直接到索引打印即可if key == "<"and int(line_list[name_list.index(name)]) < int(cond):
if content.strip() == '*':
print(line.strip())
else:
if','in content:
str1 = ''
select_name_list = content.strip().split(',')
select_name_list = [i for i in select_name_list if i != '']
for names in select_name_list:
names_index = name_list.index(names.strip())
str1 = str1 + line_list[names_index] + ','
print(str1)
else:
print(line_list[name_list.index(content.strip())])
if key == "="and line_list[name_list.index(name)] == cond:
if content.strip() == '*':
print(line.strip())
else:
if','in content:
str1 = ''
select_name_list = content.strip().split(',')
select_name_list = [i for i in select_name_list if i != '']
for names in select_name_list:
names_index = name_list.index(names.strip())
str1 = str1 + line_list[names_index] + ','
print(str1)
else:
print(line_list[name_list.index(content.strip())])
python基础代码练习if key == 'like':
name = name.strip()
cond = cond.strip()
key = key.strip()
if cond in line_list[name_list.index(name)]:
if content.strip() == '*':
print(line.strip())
else:
if','in content:
str1 = ''
select_name_list = content.strip().split(',')
select_name_list = [i for i in select_name_list if i != '']
for names in select_name_list:
names_index = name_list.index(names.strip())
str1 = str1 + line_list[names_index] + ','
print(str1)
else:
print(line_list[name_list.index(content.strip())])
else:
print('查⽆关键字')
else:
print('语句格式错误')
else:
print('查询语句输⼊错误')
def delete():
s=input('请输⼊要删除的ID,例如:delete * where id=1\n')
if'delete'and'where 'in s:
flag = False
delete_content,delete_cond=s.strip().split('where')#以where分割
if delete_content.strip() and delete_cond.strip():#判断delete_content和delete_cond是否都不为空,不为空执⾏下⾯语句
name, value = delete_cond.strip().split('=')#以=分割
医学jsp是什么意思with open('mysql', 'r', encoding='utf-8') as f:#打开⽂件
for line in f: #for循环⽂件内容
if line.strip():#如果内容不为空
line_list = line.strip().split(',')#以逗号分割,以列表的形式赋值给line_list
if line_list[name_list.index(name.strip())] == value.strip():#如果要更新的值和⽂件中对应索引的值匹配,执⾏下⾯语句
flag=True#flag改为True
if flag:#如果flag为True 执⾏下列语句
with open('mysql', 'r', encoding='utf-8') as f, open('mysql.bak', 'a', encoding='utf-8') as f1:#打开⽂件
for line in f:
if line.strip():
line_list = line.strip().split(',')
if line_list[name_list.index(name.strip())] != value.strip():#如果更新的值和⽂件中不匹配,就将内容写⼊新⽂件,匹配就不写⼊ f1.write(line)
if flag:#flag为True才会去删除⽂件,和重命名⽂件
if not flag:#如果flag为True就不执⾏,为False就执⾏
print('不到相应数据')
else:
print('语句格式错误')
else:
print('输⼊错误')
def add():
s=input('请输⼊要添加的信息,例如 add children,26,1501375,Student\n')
add_user=s.strip().split('add ')#以add+空格分割
user_str=''#定义空字符串
for i in add_user:#因为add_user是列表,所以⽤for循环转为字符串
user_str+=i
user_id=0#定义值,⽤于获取id
with open('mysql','r',encoding='utf-8') as f:#打开⽂件
for line in f:#for循环⽂件
if line.strip():
line_list = line.strip().split(',')
if int(line_list[0]) >= int(user_id):#⽂件内的id与user_id进⾏⽐较,如果⽂件内的id⼤,执⾏下⾯语句
user_id = int(line_list[0])#赋值给我们定义的user_id
user_id += 1#user_id ⾃加1
with open('mysql', 'a', encoding='utf-8') as f1:#打开新⽂件
f1.write('\n'+str(user_id)+','+user_str)#写⼊内容
print('添加成功')
def update():
s=input('请输⼊要修改的数据,例如:set age=100 where name=chen\n')
if'set'and'where'in s:
flag=False
content, condition = s.split('where')#以where分割
content=''.join(content.split('set '))#以set+空格分割
if content.strip() and condition.strip():#判断是否都不为空,不为空就执⾏下列语句
update_name, update_value = content.strip().split('=')#以=分割,赋值给update_name,updata_value,这是要更新的值
most important是什么意思name, value = condition.strip().split('=')#以=分割,赋值给name,vlue,这是条件
with open('mysql', encoding='utf-8') as f:#打开⽂件
for line in f:
if line.strip():
line_list = line.strip().split(',')#以逗号的形式分割成列表
if line_list[name_list.index(name.strip())] == value:#如果列表⾥⾯的值等于条件的值,执⾏下⾯语句
flag = True
if flag:#如果flag 为True的话,执⾏下列语句
with open('mysql', encoding='utf-8') as f, open('mysql.bak', 'w', encoding='utf-8') as f1:#打开⽂件
for line in f:
if line.strip():
line_list = line.strip().split(',')
if line_list[name_list.index(name.strip())] == value:#如果列表中的值等于条件的值
line_list[name_list.index(update_name.strip())] = update_value#那么就直接将对应索引位置的值修改为更新的值 f1.write(','.join(line_list) + '\n')#写⼊⽂件
print('更新成功')
else:#如果不等于条件的值
f1.write(line)#直接写⼊
if flag:#如果flag为True,才要删除和重命名,为False,则不⽤
if not flag:
print('⽆法到相对应的数据')
else:
print('语句格式错误')
else:
print('输⼊错误')
@auth
def main():
msg={
'1':check,
'2':delete,
'3':add,
'4':update
}
print('''
1:查询
2:删除
3:添加
4:更新
''')
num=input('请选择要执⾏的操作\n')
msg[num]()
while__name__=='__main__':
main()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论