增删查改员工信息表

import os
count=0
path=r'E:\PYTHON学习\excises\day10\infomation.txt'
def make_dic():
    '''将文件转化为列表套字典'''
    with open(path,'r',encoding='utf-8') as f:
        l1=[{'staff_id': i.split(',')[0],
             'name': i.split(',')[1],'age': i.split(',')[2],
             'phone':i.split(',')[3],'dept':i.split(',')[4],
             'enroll_date':i.split(',')[5]} for i in f]
        return l1
command=input('please input your command>>:')   #输入命令
def deco_delete_add(func):
    '''实现删除和添加新用户功能'''
    def wrapper(*args,**kwargs):
        cmd=args[0]
        if cmd.isdigit():   #判断输入的命令是查询命令还是员工ID,如果是ID删除用户
            for dic in staff_table:
                if cmd.strip() == dic['staff_id']:
                    staff_table.remove(dic)
                    with open(path,'r',encoding='utf-8') as f:
                        for line in f:
                            ls_line=line.split(',')
                            if cmd.strip() == ls_line[0]:  #查找文件中与输入的ID相符的那一行
                                pass
                            else:        # 不是ID的那些行写入一个新文件里。
                                with open(r'E:\PYTHON学习\excises\day10\infomation_copy.txt','a',encoding='utf-8') as f2:
                                    f2.write(line)
                    os.remove(path) #将旧文件删除,并将新建的文件覆盖旧文件,实现文件修改
                    os.rename('infomation_copy.txt',path)
        elif 'select' in cmd:        #不是ID,执行主函数的查询功能
            s=func(*args,**kwargs)
            return s
        elif 'UPDATE' in cmd:
            up_user(cmd)
        else:      #添加用户信息Yang Gao,25,13811223348,Student,2017-06-08
            add_user(cmd)
    return wrapper
def sl_where(cmd_x):
    global count
    l_cmd = cmd_x.strip().split('where')
    cmd_where=l_cmd[1].strip().split(maxsplit=2) #吧where后面的语句分割为3段
    cmd_where[-1]=remove_symbol(cmd_where[-1])
    for dic in staff_table:           #将列表循环,遍历每一个字典
        if cmd_where[1].strip() == '>':
            if int(dic[cmd_where[0]]) > int(cmd_where[-1]):
                count+=1
                yield(dic)
        elif cmd_where[1].strip() == '=':
            if dic[cmd_where[0]] == cmd_where[-1]:
                count+=1
                yield(dic)
        elif cmd_where[1].strip() == 'like':
            if cmd_where[-1] in dic[cmd_where[0]]:
                count += 1
                yield (dic)
def add_user(cmd_add):
        flag = 1
        cmd_phone = cmd_add.strip().split(',')[2].strip()  # 将命令中的phone提取出来
        for dic in staff_table:
            if cmd_phone == dic['phone']:
                flag = 0
                print('当前phone已存在')
        if flag:
            n = len(staff_table) + 1  # 计算要插入信息的序号
            with open(path, 'a', encoding='utf-8') as f3:
                cmd_add_write = '\n' + str(n) + ',' + cmd_add
                f3.write(cmd_add_write)
                print('add user successful')
def up_user(cmd_up):
    g2=sl_where(cmd_up)
    s = cmd_up.split('SET')[-1].split('WHERE')
    s = s[0].split(maxsplit=2) #列表['dept','=','"market"']
    s[-1]=s[-1].strip()
    s[-1]=remove_symbol(s[-1])
    for dic in g2:
        dic[s[0]]=s[-1]
    with open('copy_2.txt','w',encoding='utf-8') as f:
        for dic in staff_table:
            line='%s,%s,%s,%s,%s,%s'%(dic['staff_id'],dic['name'],dic['age'],dic['phone'],dic['dept'],dic['enroll_date'])
            f.write(line)
    os.remove(path)  # 将旧文件删除,并将新建的文件覆盖旧文件,实现文件修改
    os.rename('copy_2.txt', path)
def remove_symbol(smb):
    if '\'' in smb:
        smb1 = smb.strip('\'')
        return smb1
    elif '\"' in smb:
        smb1 = smb.strip('\"')
        return smb1
    else:
        return smb
@deco_delete_add#main=deco_delete_add(main)
def main(cmd):
    '''实现模糊查询功能'''
    sl_where(cmd)
    def sl_from(g1):
        l_cmd = cmd.strip().split('where')
        cmd_from = l_cmd[0].strip().split('from')[0]
        cmd_from=cmd_from.strip().split('select')[1].strip()  # '*' or 'name,age'
        for i in g1:
            for key in i:
                if key in cmd_from.split(',') or cmd_from == '*':
                    print(i[key], end=' ')
            print()
    g1=sl_where(cmd)
    sl_from(g1)
    print('查询到的记录为%d条' %count)
staff_table=make_dic()
main(command)
1,Alex lin,30,13651054608,market,2013-04-01
2,Jack Wang,22,13304320533,HR,2015-05-03
3,Rain Liu,25,1383235322,Sales,2016-04-22
4,Mack Cao,40,1356145343,HR,2009-03-01
5,Yu Yang,25,13811223344,Student,2017-06-16
6,Liu Yang,25,13811223345,Student,2017-06-20
7,Yang Yang,25,13811223346,Student,2017-06-20
8,Yang Yang,25,13811223347,Student,2017-06-08
9,Yang Gao,25,13811223348,Student,2017-06-08
10,Yang Gao,25,13811223350,Student,2017-06-08
11,Yang Gao,25,13811223351,Student,2017-06-08
12,Yang Gao,25,13811223352,Student,2017-06-08

 

posted @ 2017-06-17 23:43  dragonbird  阅读(513)  评论(0编辑  收藏  举报