员工信息表作业

dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}

def add_info(filename):
    user_info = []
    with open(filename, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        last_line = lines[-1]
        id_max = last_line[dic['id']]
        if id_max.isdigit():
            id = int(id_max) + 1
            user_info.append(str(id))

        name = input('请输入姓名:').strip()
        if name.isspace():
            print('姓名不能为空')
        else:
            user_info.append(name)

        age = input('请输入年龄:').strip()
        if age.isspace():
            print('年龄不能为空')
        elif not age.isdigit():
            print('年龄必须是数字')
        else:
            user_info.append(age)

        phone = input('请输入电话:').strip()
        if phone.isspace():
            print('电话不能为空')
        elif not phone.isdigit():
            print('电话必须全部由数字组成')
        else:
            user_info.append(phone)

        job = input('请输入工作:').strip()
        if job.isspace():
            print('工作不能为空')
        else:
            user_info.append(job)

    with open(filename, 'a', encoding='utf-8') as f:
        info = ','.join(user_info)
        if f.writable():
            if f.write(info) > 0:
                print('用户[%s]的信息添加成功!'%name)


def del_info():
    pass

def read_file(filename):
    with open(filename, encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            line_list = line.split(',')
            yield line_list


def cond_filter(condition, filename):
    condition = condition.strip()
    if '>=' in condition:
        condition = condition.split('>=')
        col = condition[0].strip()
        val = condition[1].strip()

        g = read_file(filename)
        for line_list in g:
            if int(line_list[dic[col]]) >= int(val):
                yield line_list
    elif '<=' in condition:
        condition = condition.split('<=')
        col = condition[0].strip()
        val = condition[1].strip()

        g = read_file(filename)
        for line_list in g:
            if int(line_list[dic[col]]) <= int(val):
                yield line_list
    elif '>' in condition:
        condition = condition.split('>')
        col = condition[0].strip()
        val = condition[1].strip()

        g = read_file(filename)
        for line_list in g:
            if int(line_list[dic[col]]) > int(val):
                yield line_list
    elif '<' in condition:
        condition = condition.split('<')
        col = condition[0].strip()
        val = condition[1].strip()

        g = read_file(filename)
        for line_list in g:
            if int(line_list[dic[col]]) < int(val):
                yield line_list
    elif 'like' in condition:
        condition = condition.split('like')
        col = condition[0].strip()
        val = condition[1].strip()

        g = read_file(filename)
        for line_list in g:
            if val in line_list[dic[col]]:
                yield line_list
    else:
        print('查询暂时只支持>=、<=、>、<、like')


def print_info(col_lst, staff):
    if '*' in col_lst:
        col_lst = dic.keys()

    for  i in col_lst:
        print(i, end='\t\t\t')
    print('')

    for staff_info in staff:
        for i in col_lst:
            print(staff_info[dic[i.strip()]], end='\t\t\t')
        print('')


print("请选择你的功能:【1】新增用户信息 【2】删除用户信息 【3】查询用户信息 [exit] 退出")
choose = input("请选择你的功能:").strip()
while True:
    ret = input(">>>").lower()
    if 'exit'.lower() == ret.strip():
        break

    if choose == '3':
        if 'where' in ret:
            select, where = ret.split('where')
            select, table = select.split('from')
            cols = select.replace('select', '').strip()
            cols = cols.split(',')
            g = cond_filter(where.strip(), table.strip())
            print_info(cols, g)
        else:
            select, table = ret.split('from')
            cols = select.replace('select', '').strip()
            cols = cols.split(',')
            g = read_file(table.strip())
            print_info(cols, g)
        ret = input(">>>")
    elif choose == '1':
        add_info('userinfo')
        ret = input(">>>")
posted @ 2020-01-19 15:39  飞鸽子  阅读(109)  评论(0编辑  收藏  举报