大作业:实现员工信息表
利用txt文件存储格式如下: 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来修改列名对应的值为“新的值” 注意:要想操作员工信息表,必须先登录,登陆认证需要用装饰器完成 其他需求尽量用函数实现'''
思路流程图:
写了一点,没写完,写了一点就这么费劲,是不是没救了
import os import sys os.getcwd() def openread(): data_list = [] f = open('emp.txt','r', encoding='UTF-8') for line in f: l = line.strip() line_list = l.split(",") data_list.append(line_list) return data_list f.close() #print(openread()) def select(): result = [] emp_input = input('请按格式输入:') emp_input_one,emp_input_two= emp_input.split('where') list_age = openread() if 'select name, age' == emp_input_one.strip(): emp_input_two = emp_input_two.replace('age>','') for n in range(0,len(list_age)): if n < len(list_age) and int(list_age[n][2]) > int(emp_input_two): result.append(list_age[n]) return result elif 'select *' == emp_input_one.strip(): if 'job' in emp_input_two: emp_input_two = emp_input_two.replace('job=','') for n in range(0,len(list_age)): if n < len(list_age) and emp_input_two.strip() == list_age[n][4].strip(): result.append(list_age[n]) return result elif 'phone' in emp_input_two: emp_input_two = emp_input_two.replace('phone like','') for n in range(0,len(list_age)): if n < len(list_age) and emp_input_two.strip() in list_age[n][3].strip(): result.append(list_age[n]) return result # elif print(select()) #select name, age where age>10