作業,員工資訊表
阿新 • • 發佈:2018-12-22
大作業:實現員工資訊表
利用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