Python训练题-员工信息表
2020-03-10 本文已影响0人
复苏的兵马俑
实现员工信息表:
文件存储格式如下:
id,name,age,phone,job
1,Alex,22,13651054608,IT
2,Egon,23,13304320533,Teacher
3,Nezha,25,13332355322,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来修改列名对应的值为'新的值'。
注意:要想操作员工信息表,必须先登录,登录认证需要用装饰器完成,其他需求尽量用函数实现。
答案:
文件“UserInfo.txt”中的内容如下:
1,Alex,22,13651054608,IT
2,Egon,23,13304320533,Teacher
3,Nezha,25,13332355322,IT
a、实现三种查询
代码一:
dic = {
'id': 0,
'name':1,
'age':2,
'phone':3,
'job':4
}
def get_line(filename):
with open(filename,encoding = 'utf-8') as f:
for line in f:
line = line.strip()
line_lst = line.split(',')
yield line_lst
def condition_filter(condition):
'''条件筛选'''
condition = condition.strip()
if '>' in condition:
col,val = condition.split('>')
col = col.strip()
val = val.strip()
g = get_line('UserInfo.txt')
for line_lst in g:
if int(line_lst[dic[col]]) > int(val):
yield line_lst
elif '<' in condition:
col,val = condition.split('<')
col = col.strip()
val = val.strip()
g = get_line('UserInfo.txt')
for line_lst in g:
if int(line_lst[dic[col]]) < int(val):
yield line_lst
elif '=' in condition:
col,val = condition.split('=')
col = col.strip()
val = val.strip()
g = get_line('UserInfo.txt')
for line_lst in g:
if line_lst[dic[col]] == val:
yield line_lst
elif 'like' in condition:
col,val = condition.split('like')
col = col.strip()
val = val.strip()
g = get_line('UserInfo.txt')
for line_lst in g:
if val in line_lst[dic[col]]:
yield line_lst
else:
print("查询条件有误,请重新输入!")
def views(view_lst,staff_g):
'''显示符合条件的员工信息'''
if '*' in view_lst:
view_lst =dic.keys()
for staff_info in staff_g:
for i in view_lst:
print(staff_info[dic[i]],end = ' ')
print('')
while 1:
ret = input("请输入查询条件:")
if ret.upper() == 'Q':
print("结束查询!")
break
else:
view,condition = ret.split('where')
view = view.replace('select','').strip()
view_lst = view.split(',')
g = condition_filter(condition)
views(view_lst,g)
运行结果:
请输入查询条件:select name,age where age > 22
Egon 23
Nezha 25
请输入查询条件:select name,age where age = 22
Alex 22
请输入查询条件:select * where job = IT
1 Alex 22 13651054608 IT
3 Nezha 25 13332355322 IT
请输入查询条件:select * where job = Teacher
2 Egon 23 13304320533 Teacher
请输入查询条件:select * where phone like 133
2 Egon 23 13304320533 Teacher
3 Nezha 25 13332355322 IT
请输入查询条件:select * where phone like 136
1 Alex 22 13651054608 IT
请输入查询条件:select * where phone in 133
查询条件有误,请重新输入!
请输入查询条件:q
结束查询!
代码二:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Time : 2020-03-10 12:00
@Auth : Kevin.hu
@IDE : PyCharm
@Motto: ABC(Always Be Coding)
"""
column_dic ={
'id': 0,
'name': 1,
'age': 2,
'phone': 3,
'job': 4
}
def filter_handler(operate,con):
'''
进行筛选工作
:param operate:用户要进行的操作是>、<、=、like
:param con: 用户输入的where条件
:return: 被选中的所有航组成的列表,其中每一行都是一个列表
'''
selected_lst = []
col,val = con.split(operate)
col = col.strip()
val = val.strip()
judge = 'int(line_lst[column_dic[col]]) %s int(val)' %operate if operate == '<' or operate == '>' else 'val %s line_lst[column_dic[col]]' %operate
f = open('UserInfo.txt', encoding = 'utf-8')
for line in f:
line_lst = line.strip().split(',')
if eval(judge):
selected_lst.append(line_lst)
f.close()
return selected_lst
def get_selected_line(con):
'''
获取所有要查找的行,并将每一行作为一个列表项存储在selected_lst中
:param con: 用户输入的where条件
:return: 存储了符合条件的行的列表
'''
if '>' in con:
selected_lst = filter_handler('>', con)
elif '<' in con:
selected_lst = filter_handler('<', con)
elif '=' in con:
selected_lst = filter_handler('==', con.replace('=', '=='))
elif 'like' in con:
selected_lst = filter_handler('in', con.replace('like', 'in'))
else:
return None
return selected_lst
def get_show_lst(col_condition):
'''
获取要展示的列名
:param col_condition: 用户输入的select条件
:return: 列名组成的字典
'''
col_info_lst = col_condition.strip().split('select')
col_info_lst = [col_info_item for col_info_item in col_info_lst if col_info_item.strip()]
if col_info_lst:
col_info = col_info_lst[0].strip()
if '*' == col_info:
return column_dic.keys()
elif col_info:
ret = col_info.split(',')
return [item.strip() for item in ret]
else:
print(col_info)
def show(selected_lst,shou_lst):
'''
展示符合条件的内容
:param selected_lst:符合条件的行的列表
:param shou_lst: 所有要展示的字段
:return: 无
'''
for selected_item in selected_lst:
for col in show_lst:
print(selected_item[column_dic[col]], end = ' ')
print('')
flag = True
while flag:
# 接收用户的指令
condition = input('请输入查询条件,输入‘q’或‘Q’退出查询:')
if condition.upper() == 'Q':
print('结束查询!')
flag = False
else:
# 解析用户的指令
ret = condition.split('where')
con = ret[1].strip()
#根据select条件解析用户需要展示的内容
show_lst = get_show_lst(ret[0])
# 根据where条件解析筛选用户想查找的内容
selected_lst = get_selected_line(con) # selected_lst中存储了所有符合条件的内容
if selected_lst == None:
print("查询条件有误,请重新输入!")
continue
else:
# 将符合条件的内容按照用户的需求展示出来
show(selected_lst,show_lst)
运行结果:
请输入查询条件,输入‘q’或‘Q’退出查询:select name,age where age > 22
Egon 23
Nezha 25
请输入查询条件,输入‘q’或‘Q’退出查询:select name,age where age = 22
Alex 22
请输入查询条件,输入‘q’或‘Q’退出查询:select * where job = IT
1 Alex 22 13651054608 IT
3 Nezha 25 13332355322 IT
请输入查询条件,输入‘q’或‘Q’退出查询:select * where job = Teacher
2 Egon 23 13304320533 Teacher
请输入查询条件,输入‘q’或‘Q’退出查询:select * where phone like 133
2 Egon 23 13304320533 Teacher
3 Nezha 25 13332355322 IT
请输入查询条件,输入‘q’或‘Q’退出查询:select * where phone like 136
1 Alex 22 13651054608 IT
请输入查询条件,输入‘q’或‘Q’退出查询:select * where phone in 133
查询条件有误,请重新输入!
请输入查询条件,输入‘q’或‘Q’退出查询:q
结束查询!