Python3.5 day4做業:對員工信息文件,實現增刪改查操做。

需求:app

     1.可進行模糊查詢,語法至少支持下面3種:運維

         1.select name,age from staff_table where age > 22fetch

         2.select  * from staff_table where dept = ITspa

         3.查到的信息,打印後,最後面還要顯示查到的條數 code

     2.可建立新員工紀錄,以phone作惟一鍵,staff_id需自增blog

     3.可刪除指定員工信息紀錄,輸入員工id,便可刪除ip

     4.可修改員工信息,語法以下:utf-8

         1.UPDATE staff_table SET dept= 無 where dept = ITrem

注意:請輸入正確的語句進行查詢,不然會報錯。input

#_*_coding:utf-8_*_
import os
def fetch(data):
    #查詢語法一:select name,age from staff_table where age > 22
    #查詢語法二:select * from staff_table where dept = IT
    data1 = data.split(" ")
    directory = ["staff_id", "name", "age", "phone", "dept", "enroll-date"]
    if data == ("select name,age from staff_table where age > %s" %(data1[7])):
                with open("xinxi", encoding="utf-8") as f:
                    list = []
                    list1 = []
                    list2 = []
                    for line in f:
                        i = line.strip().split(",")
                        w = i[1]
                        e = i[2]
                        a = [w, e]
                        if e > data1[7]:
                            list2.append(a)
                    for i in list2:
                        print(i)
                    print("查詢到 %s 條符合的信息" %len(list2))
    else:
         with open("xinxi", encoding="utf-8") as f:
             list = []
             for line in f:
                 i = line.strip().split(",")
                 q = i[0]
                 w = i[1]
                 e = i[2]
                 r = i[3]
                 t = i[4]
                 y = i[5]
                 if data == ("select * from staff_table where %s = %s" % (data1[5], i[(directory.index(data1[5]))])):
                         list.append(i)
                 else:
                     continue
             for j in list:
                  print('.'.join(j))
             print("查詢到 %s 條符合的信息" %len(list))
             return 0
def add(data):
    #添加語法: name,age,phone,dept,enroll-date  (例如:王新凱,22,11111111111,IT,2015-10-31)
    data1 = data.split(",")
    f=  open("xinxi",encoding="utf-8")
    all_list = []
    list = []
    phone_list = []
    for line in f:
            i = line.strip().split(",")
            q = i[3]
            phone_list.append(q)

    if data1[2] in phone_list:
                print("手機號已存在")
                f.close()
    else:
                f1 = open("xinxi", "r+", encoding="utf-8")
                for line in f1:
                        lines = line.strip().split(",")
                        # print(lines)
                        list.append(lines)
                        i = line.strip().split(",")
                w = str(int(list[-1][0]) + 1)
                data1.insert(0, w)
                print(data1)
                data1 = ','.join(data1)
                f1.write("\n")
                f1.write(data1)
                f1.close()
                print("添加成功!!!")
def remove(data):
    #刪除語法:delete from staff_table where staff_id = 12
   data1 = data.split(" ")
   if data == ("delete from staff_table where staff_id = %s" %data1[6]):
       with open("xinxi", encoding="utf-8") as f:
           list = []
           for line in f:
               i = line.strip().split(",")
               i1 = line.splitlines()
               q = i[0]
               if data1[6] == q:
                   i2 = ','.join(i1)
                   print(i2)
                   list.append(i)
       a = i2
       f = open("xinxi", encoding="utf-8")
       f1 = open("back", "a+", encoding="utf-8")
       for i in f:
           if a in i:
               i = i.replace(a, "").strip()
           f1.write(i)
           f1.flush()
       f.close()
       f1.close()
   os.remove("xinxi")
   os.rename("back","xinxi")
   print("刪除成功!!!")
   return
def change(data):
    #修改請輸入(注意空格和沒有引號):UPDATE staff_table SET dept = IT where dept = 運維
    data1 = data.split(" ")
    print(data1)
    # a =','.join(data1[3])
    # print(a)
    directory = ["staff_id", "name", "age", "phone", "dept", "enroll-date"]
    var = int(directory.index(data1[3]))
    with open("xinxi", encoding="utf-8") as f,\
          open("back","w",encoding="utf-8")as f1:
        for line in f:
            lines = line.strip()
            print(lines)
            if  data1[5] in lines:
                lines = lines.replace(data1[5],data1[9])
            f1.write(lines)
            f1.write("\n")
            f1.flush()
    os.remove("xinxi")
    os.rename("back","xinxi")
    print("修改爲功!!!")

if __name__ == "__main__":
    msg = """
    1:查詢
    2:添加
    3:刪除
    4:修改
    5:退出
    """
    msg_dict = {
        "1": fetch,
        "2": add,
        "3": remove,
        "4": change,
        "5": exit,
    }
    while True:
        print(msg)
        choice = input("輸入序號>>:")
        if len(choice) == 0 or choice not in msg_dict: continue
        if choice =='5':break
        data = input("請輸入數據>>:").strip()
        msg_dict[choice](data)
相關文章
相關標籤/搜索