dic = {'name': 1,'id':0,'age':2,'phone':3,'job':4}# 读取文件 —— 将文件中的内容整理到内存里def get_line(filename): ''' 读取文件中的每一行 :param filename:文件名 :return:生成器,文件中每一行的生成器 ''' with open(filename,encoding='utf-8') as f: for line in f: line = line.strip() line_lst = line.split(',') yield line_lstdef condition_filter(condition): ''' 根据条件_筛选 :param condition:用户输入的条件'age'>20 :return:符合条件的行列表 ''' 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_lstdef views(view_lst,staff_g): #view_lst = ['name','age'] ''' 展示符合条件的员工信息 :param view_lst:['name','age'] :param staff_g:符合条件的员工信息生成器 :return:None ''' if '*' in view_lst: view_lst = dic.keys() for staff_info in staff_g: #staff_info = [1,Alex,22,13651054608,IT] for i in view_lst: print(staff_info[dic[i]],end=' ') print('')# 接收用户的信息 —— 分析信息# ret = input('>>> ')ret = 'select name,age where age>22'view,condition = ret.split('where') #根据where切割用户输入得到'select name,age ',' age>22'view = view.replace('select','').strip() #select name,age '去掉select和空格view_lst = view.split(',') #'name,age'切割成列表[name,age]print(view_lst,condition)g = condition_filter(condition) #调用筛选条件函数,找到符合用户输入条件的行(生成器)views(view_lst,g) #展示符合条件的数据