day12 python作业:员工信息表

时间:2022-07-30 20:44:14

作业要求:

周末大作业:实现员工信息表
文件存储格式如下:
id,name,age,phone,job
1,Alex,22,13651054608,IT
2,Egon,23,13304320533,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来修改列名对应的值为“新的值”

注意:要想操作员工信息表,必须先登录,登陆认证需要用装饰器完成.其他需求尽量用函数实现

作业代码:

自己鼓捣大半天,把查询功能实现了,代码没有进行优化,回头再优化代码完成进阶要求.

FLAG = False
def wrapper(func):
def inner(*args,**kwargs):
global FLAG
'''登录程序'''
if FLAG:
ret = func(*args,**kwargs)
return ret
else:
username = input('username:')
password = input('password:')
if username == 'pengzhaoyang' and password == '':
FLAG = True
ret = func(*args,**kwargs)
return ret
else:
print('登录失败')
return inner @wrapper
def dayu():
keys = vkey[0].split(',')
sele = vkey[1].split(">")
with open('message','r',encoding='utf-8') as f:
for line in f:
lis = line.strip().split(',')
dic = {}
s = ''
for i in range(len(lis)):
dic[doc[i]] = lis[i]
if int(dic[sele[0]]) > int(sele[1]):
if keys[0] != '*':
for key in keys:
s += dic[key] + ','
else:
s = line.strip()
print(s) @wrapper
def xiaoyu():
keys = vkey[0].split(',')
sele = vkey[1].split("<")
with open('message','r',encoding='utf-8') as f:
for line in f:
lis = line.strip().split(',')
dic = {}
s = ''
for i in range(len(lis)):
dic[doc[i]] = lis[i]
if int(dic[sele[0]]) < int(sele[1]):
if keys[0] != '*':
for key in keys:
s += dic[key] + ','
else:
s = line.strip()
print(s) @wrapper
def dengyu():
keys = vkey[0].split(',')
sele = vkey[1].split("=")
with open('message','r',encoding='utf-8') as f:
for line in f:
lis = line.strip().split(',')
dic = {}
s = ''
for i in range(len(lis)):
dic[doc[i]] = lis[i]
if dic[sele[0]] == sele[1]:
if keys[0] != '*':
for key in keys:
s += dic[key] + ','
else:
s = line.strip()
print(s) @wrapper
def like():
keys = vkey[0].split(',')
with open('message','r',encoding='utf-8') as f:
for line in f:
lis = line.strip().split(',')
dic = {}
s = ''
for i in range(len(lis)):
dic[doc[i]] = lis[i]
if vkey[-1] in dic[vkey[1]]:
if keys[0] != '*':
for key in keys:
s += dic[key] + ','
else:
s = line.strip()
print(s) doc = ['id', 'name', 'age', 'phone', 'job'] while True:
sel = input('>>>') # 输入查询命令
if 'select' in sel and 'where' in sel:
vkey = sel.split()[1::2]
if '>' in sel:
dayu()
elif '<' in sel:
xiaoyu()
elif '=' in sel:
dengyu()
else:
like()
else:
print('语法错误!!!')