Python_查找员工信息

'''
查找出userinfo文件中年龄大于22岁的员工姓名和年龄
1,Alex,22,13651054608,IT
2,Egon,23,13304320533,Tearcher
3,nezha,25,1333235322,IT
select name,age where age>22
'''
# 员工信息表:完善代码,背下来给代码加注释
dic = {'name': 1,'id':0,'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

# get_line('userinfo')

def condition_filter(condition):
    '''条件筛选'''
    condition = condition.strip()
    if '>' in condition:
        col,val = condition.split('>')
        g = get_line('userinfo')
        for line_lst in g:
            if int(line_lst[dic[col]]) > int(val):
                yield line_lst

def views(view_lst,staff_g):
    '''展示符合条件的员工信息'''
    if '*' in view_lst:
        view_lst = dic.keys()
    for staff_info in staff_g:
        #['name', 'age']
        info = []
        for i in view_lst:
            # print(staff_info[dic[i]], end=' ')
            info.append(staff_info[dic[i]])
        # print('')
        print(info)

#接受用户信息 —— 分析 信息
# ret = input('>>>')
ret = 'select name,job,age where age>22'
view, condition = ret.split('where')
view = view.replace('select','').strip()
view_lst = view.split(',')
# print(view_lst, condition)
g = condition_filter(condition)
views(view_lst,g)

# print(view_lst,condition)
# print('aaa********aaab'.strip('ab')) # ********
# print(view.split('select ').pop(0))
# print(view.replace('select','').strip())

猜你喜欢

转载自www.cnblogs.com/LXL616/p/10699624.html