第四十七天Python学习记录

作业

现要求你写一个简单的员工信息增删改查程序,需求如下:

当然此表你在文件存储时可以这样表示

1,Alex Li,22,13651054608,IT,2013-04-01
2,Jack Wang,28,13451024608,HR,2015-01-07
3,Rain Wang,21,13451054608,IT,2017-04-01
4,Mack Qiao,44,15653354208,Sales,2016-02-01
5,Rachel Chen,23,13351024606,IT,2013-03-16
6,Eric Liu,19,18531054602,Marketing,2012-12-01
7,Chao Zhang,21,13235324334,Administration,2011-08-08
8,Kevin Chen,22,13151054603,Sales,2013-04-01
9,Shit Wen,20,13351024602,IT,2017-07-03
10,Shanshan Du,26,13698424612,Operation,2017-07-02

1.可进行模糊查询,语法至少支持下面3种查询语法:

find name,age from staff_table where age > 22

find * from staff_table where dept = "IT"

find * from staff_table where enroll_date like "2013"

2.可创建新员工纪录,以phone做唯一键(即不允许表里有手机号重复的情况),staff_id需自增

语法: add staff_table Alex Li,25,134435344,IT,2015-10-29

3.可删除指定员工信息纪录,输入员工id,即可删除

语法: del from staff where  id=3

4.可修改员工信息,语法如下:

UPDATE staff_table SET dept="Market" WHERE  dept = "IT" 把所有dept=IT的纪录的dept改成Market
UPDATE staff_table SET age=25 WHERE  name = "Alex Li"  把name=Alex Li的纪录的年龄改成25

5.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。

注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!

 

  1 import os
  2 
  3 
  4 def table_staff():  # 将txt文本信息转换成列表,并向外部函数返回
  5     data_file = open('data.txt', 'r+')
  6     data = data_file.readlines()
  7     data_list = []
  8     for i in data:
  9         data_list.append(str(i).split(','))
 10     return data_list
 11 
 12 
 13 def del_update_share(data_value, data_file1):  # 删除更改共享函数,实现去掉最末行换行符、转换、写文件和更名的功能
 14 
 15     a = (','.join(data_value[len(data_value) - 1]).replace("\n", "")).split(",")  # 去掉最末行换行符
 16     data_value[len(data_value) - 1] = a
 17 
 18     for i in data_value:
 19         str_i = ','.join(i)
 20         data_file1.write(str_i)
 21 
 22     data_file1.flush()
 23     data_file1.close()
 24 
 25     os.remove("data.txt")
 26     os.rename("data1.txt", "data.txt")
 27 
 28 
 29 def query_staff():  # 员工信息查询,通过切片方式获取用户输入关键字,根据关键字获取用户查询信息
 30     data_value = table_staff()
 31     # print(data_value)
 32     input_query = input("请输入查询语句:")
 33     num1 = input_query[0:input_query.rfind('from', 0) + -1][5:]  # 截取 name,age
 34     num2 = input_query[input_query.rfind('>', 1) + 2:]  # 截取22
 35     num3 = input_query[input_query.rfind('where', 1) + 6:]  # 截取 dept = "IT"
 36 
 37     if num1 == '*':
 38         value = 'dept = "IT"'
 39         if value == num3:
 40             count = 0
 41             for k in data_value:
 42                 if "IT" in k[4]:
 43                     str_i = ','.join(k).replace("\n", "").replace(",", " ")
 44                     print(str_i)
 45                     count += 1
 46             print("查询到%s条结果!" % count)
 47 
 48         else:
 49             count = 0
 50             for k in data_value:
 51                 if "2013" in k[5]:
 52                     str_i = ','.join(k).replace("\n", "").replace(",", " ")
 53                     print(str_i)
 54                     count += 1
 55             print("查询到%s条结果!" % count)
 56 
 57     else:
 58         count = 0
 59         for k in data_value:
 60             if int(k[2]) > int(num2):
 61                 print(k[1], k[2])
 62                 count += 1
 63         print("查询到%s条结果!" % count)
 64 
 65 
 66 def add_staff():  # 增加员工信息,phone不能重复,通过a+在data.txt后追加,
 67     data_value = table_staff()
 68 
 69     input_add = input("请输入新增语句:")
 70     input_list = list(input_add[16:].split(','))
 71 
 72     phone_list = []
 73     for i in data_value:
 74         phone_list.append(i[3])
 75 
 76     if input_list[2] in phone_list:
 77         print("手机号已存在!")
 78         exit()
 79 
 80     else:
 81         staff_id = len(data_value) + 1
 82         input_list.insert(0, str(staff_id))
 83         input_list_str = ','.join(input_list)
 84 
 85         data_file = open('data.txt', 'a+')
 86         data_file.write("\n")
 87         data_file.write(input_list_str)
 88         data_file.close()
 89 
 90         print("新增1条员工记录!")
 91 
 92 
 93 def remove_staff():  # 删除员工信息,利用输入的切片信息作为判断条件,以占硬盘的方式的删除
 94     data_value = table_staff()
 95     input_remove = input('请输入删除语句:')
 96     id_value = input_remove[input_remove.rfind('=', 1) + 2:]
 97 
 98     if input_remove[:32] == "del from staff_table where id = ":
 99         data_file1 = open('data1.txt', 'a+', encoding='utf-8')
100 
101         del data_value[int(id_value) - 1]
102         del_update_share(data_value, data_file1)
103 
104         print("删除1条员工记录!")
105 
106     else:
107         print("您输入的格式不正确!")
108 
109 
110 def update_staff():  # 修改员工信息,利用输入的切片信息作为判断条件,以占硬盘的方式的修改
111     data_value = table_staff()
112 
113     input_update = input('请输入修改语句:')
114     if input_update[:22] == "UPDATE staff_table SET":
115         num1 = input_update[:35][29:]  # 截取Market
116         num2 = input_update[:29][27:]  # 截取age
117 
118         data_file1 = open('data1.txt', 'a+', encoding='utf-8')
119         if input_update[:28] == "UPDATE staff_table SET dept=":
120             count = 0
121             for i in data_value:
122                 if i[4] == 'IT':
123                     i[4] = num1
124                     count += 1
125 
126             del_update_share(data_value, data_file1)
127             print("您修改了%s条员工信息!" % count)
128 
129         elif input_update[:27] == "UPDATE staff_table SET age=":
130             count = 0
131             for i in data_value:
132                 if i[1] == 'Alex Li':
133                     i[2] = num2
134                     count += 1
135 
136             del_update_share(data_value, data_file1)
137             print("您修改了%s条员工信息!" % count)
138 
139         else:
140             print("您输入的格式不正确!")
141 
142     else:
143         print("您输入的格式不正确!")
144 
145 
146 while True:  # 一直循环主程序,直到用户选择功能后退出
147     print(
148         '欢迎进入员工信息增删改查程序'.center(50, '*'), """
149     1. 模糊查询  (语法: find name,age from staff_table where age > 22 
150                        find * from staff_table where dept = "IT"
151                        find * from staff_table where enroll_date like "2013" )
152                        
153     2. 新增信息  (语法: add staff_table Alex,25,134435344,IT,2015-10-29 )
154     
155     3. 删除信息  (语法: del from staff_table where id = 3)
156     
157     4. 修改信息  (语法: UPDATE staff_table SET dept="Market" WHERE dept = "IT"
158                        UPDATE staff_table SET age=25 WHERE name = "Alex Li" )
159     5. 退出                  
160     """,
161         '欢迎进入员工信息增删改查程序'.center(50, '*'))
162 
163     choice = int(input("请选择要进入的功能序号:"))
164     if choice == 1:
165         print("成功进入查询功能".center(50, '*'))
166         query_staff()
167         break
168 
169     elif choice == 2:
170         print("成功进入新增功能".center(50, '*'))
171         add_staff()
172         break
173 
174     elif choice == 3:
175         print("成功进入删除功能".center(50, '*'))
176         remove_staff()
177         break
178 
179     elif choice == 4:
180         print("成功进入新增功能".center(50, '*'))
181         update_staff()
182         break
183 
184     elif choice == 5:
185         print("退出成功!")
186         break
187     else:
188         print("您输入的信息有误!")

注意:此代码有个bug,只能顺序增加和删除,不能从中间删除和添加,从中间删除后,会导致staff_id 和 索引的混乱,

posted @ 2018-02-24 09:01  xudachen  阅读(223)  评论(0编辑  收藏  举报