python基礎-對員工信息文件,實現增刪改查操作


需求:

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

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

         2.select  * from staff_table where dept = IT

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

     2.可創建新員工紀錄,以phone做唯一鍵,staff_id需自增

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

     4.可修改員工信息,語法如下:

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

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

實現代碼如下:

  1 #_*_coding:utf-8_*_
  2 import os
  3 def fetch(data):
  4     #查詢語法一:select name,age from staff_table where age > 22
  5     #查詢語法二:select * from staff_table where dept = IT
  6     data1 = data.split(" ")
  7     directory = ["staff_id", "name", "age", "phone", "dept", "enroll-date"]
  8     if data == ("select name,age from staff_table where age > %s" %(data1[7])):
  9                 with open("xinxi", encoding="utf-8") as f:
 10                     list = []
 11                     list1 = []
 12                     list2 = []
 13                     for line in f:
 14                         i = line.strip().split(",")
 15                         w = i[1]
 16                         e = i[2]
 17                         a = [w, e]
 18                         if e > data1[7]:
 19                             list2.append(a)
 20                     for i in list2:
 21                         print(i)
 22                     print("查詢到 %s 條符合的信息" %len(list2))
 23     else:
 24          with open("xinxi", encoding="utf-8") as f:
 25              list = []
 26              for line in f:
 27                  i = line.strip().split(",")
 28                  q = i[0]
 29                  w = i[1]
 30                  e = i[2]
 31                  r = i[3]
 32                  t = i[4]
 33                  y = i[5]
 34                  if data == ("select * from staff_table where %s = %s" % (data1[5], i[(directory.index(data1[5]))])):
 35                          list.append(i)
 36                  else:
 37                      continue
 38              for j in list:
 39                   print('.'.join(j))
 40              print("查詢到 %s 條符合的信息" %len(list))
 41              return 0
 42 def add(data):
 43     #添加語法: name,age,phone,dept,enroll-date  (例如:孫凱,22,11111111111,IT,2016-12-12)
 44     data1 = data.split(",")
 45     f=  open("xinxi",encoding="utf-8")
 46     all_list = []
 47     list = []
 48     phone_list = []
 49     for line in f:
 50             i = line.strip().split(",")
 51             q = i[3]
 52             phone_list.append(q)
 53 
 54     if data1[2] in phone_list:
 55                 print("手機號已存在")
 56                 f.close()
 57     else:
 58                 f1 = open("xinxi", "r+", encoding="utf-8")
 59                 for line in f1:
 60                         lines = line.strip().split(",")
 61                         # print(lines)
 62                         list.append(lines)
 63                         i = line.strip().split(",")
 64                 w = str(int(list[-1][0]) + 1)
 65                 data1.insert(0, w)
 66                 print(data1)
 67                 data1 = ','.join(data1)
 68                 f1.write("\n")
 69                 f1.write(data1)
 70                 f1.close()
 71                 print("添加成功!!!")
 72 def remove(data):
 73     #刪除語法:delete from staff_table where staff_id = 12
 74    data1 = data.split(" ")
 75    if data == ("delete from staff_table where staff_id = %s" %data1[6]):
 76        with open("xinxi", encoding="utf-8") as f:
 77            list = []
 78            for line in f:
 79                i = line.strip().split(",")
 80                i1 = line.splitlines()
 81                q = i[0]
 82                if data1[6] == q:
 83                    i2 = ','.join(i1)
 84                    print(i2)
 85                    list.append(i)
 86        a = i2
 87        f = open("xinxi", encoding="utf-8")
 88        f1 = open("back", "a+", encoding="utf-8")
 89        for i in f:
 90            if a in i:
 91                i = i.replace(a, "").strip()
 92            f1.write(i)
 93            f1.flush()
 94        f.close()
 95        f1.close()
 96    os.remove("xinxi")
 97    os.rename("back","xinxi")
 98    print("刪除成功!!!")
 99    return
100 def change(data):
101     #修改請輸入(注意空格和沒有引號):UPDATE staff_table SET dept = IT where dept = 運維
102     data1 = data.split(" ")
103     print(data1)
104     # a =','.join(data1[3])
105     # print(a)
106     directory = ["staff_id", "name", "age", "phone", "dept", "enroll-date"]
107     var = int(directory.index(data1[3]))
108     with open("xinxi", encoding="utf-8") as f,\
109           open("back","w",encoding="utf-8")as f1:
110         for line in f:
111             lines = line.strip()
112             print(lines)
113             if  data1[5] in lines:
114                 lines = lines.replace(data1[5],data1[9])
115             f1.write(lines)
116             f1.write("\n")
117             f1.flush()
118     os.remove("xinxi")
119     os.rename("back","xinxi")
120     print("修改成功!!!")
121 
122 if __name__ == "__main__":
123     msg = """
124     1:查詢
125     2:添加
126     3:刪除
127     4:修改
128     5:退出
129     """
130     msg_dict = {
131         "1": fetch,
132         "2": add,
133         "3": remove,
134         "4": change,
135         "5": exit,
136     }
137     while True:
138         print(msg)
139         choice = input("輸入序號>>:")
140         if len(choice) == 0 or choice not in msg_dict: continue
141         if choice =='5':break
142         data = input("請輸入數據>>:").strip()
143         msg_dict[choice](data)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM