python实现员工信息表

2022/2/23 20:22:20

本文主要是介绍python实现员工信息表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

学习python时,看到的一个题目
第一次写博客, 有误的地方还请大佬们指正,十分感谢~

要求如下
'''
文件存储格式如下:
id,name,age,phone,job(这行不需要写)
1,alice,22,13651156619,IT
2,ben,23,13310321345,Teacher
3,xiaoming,25,1331235342,IT
...

实现增删改查
1.查询:支持三种语法
select 列名1,列名2,... where 列名条件
支持大于小于等于,以及模糊查找(like)
select name,age where age>22    输出年龄大于22的人的名字和年龄
select * where job=IT           输出工作是IT的人的所有信息
select * where phone like 133   输出电话号包含133的人的所有信息

之后的增删改必须先登录,登录认证需要用装饰器完成

2.增:创建新员工记录,id要顺序增加

3.删除指定员工,直接输入员工id或名字即可

4.修改员工信息
语法: set 列名='新的值' where 条件
先用where查找对应人的信息,再用set修改列名对应的值为'新的值' 
'''

  1 import os
  2 FLAG = False
  3 tip = True
  4 Account = '登入账号存放位置'
  5 FileName = '员工信息文件存放位置'
  6 def login(func):
  7     def inner(*args, **kwargs):
  8         if FLAG:
  9             ret = func(*args, **kwargs)
 10             return ret
 11         else:
 12             username = input('请登录后执行改操作!\n用户名:')
 13             passwd = input('密码:')
 14             if username and passwd:
 15                 with open(Account,'r',encoding='utf-8') as f:
 16                     read_lines = f.readlines()
 17                 if username == read_lines[0].strip() and passwd == read_lines[1].strip():
 18                     res = func(*args, **kwargs)
 19                     return ['登入成功', res]
 20                 else:
 21                     return '登入失败'
 22             else:
 23                 return '登入失败'
 24     return inner
 25 
 26 @login
 27 def select_operate(rec):
 28     '''
 29     查询操作函数第二版
 30     接收处理后的列表
 31     返回一个表示结果的列表
 32     '''
 33     rol_name = ['id', 'name', 'age', 'phone', 'job', '*']
 34     res = []
 35     def judge(rol_suoyin, tiaojian = rec[3]):
 36         if '>=' in tiaojian:
 37             tiaojian = tiaojian.split('>=')
 38             tiaojian_before = tiaojian[0]
 39             tiaojian_after = tiaojian[1]
 40             if tiaojian_before in rol_name and tiaojian_after.isdigit():
 41                 tiaojian_suoyin = rol_name.index(tiaojian_before)
 42                 with open(FileName, 'r', encoding='utf-8') as f:
 43                     for line in f:
 44                         line = line.strip().split(',')
 45                         if int(line[tiaojian_suoyin]) >= int(tiaojian_after):
 46                             if rol_suoyin == 5:
 47                                 res.append(line)
 48                             else:
 49                                 res.append(line[rol_suoyin])
 50                 return res
 51             else:
 52                 return '条件输入有误'
 53         elif '<=' in tiaojian:
 54             tiaojian = tiaojian.split('<=')
 55             tiaojian_before = tiaojian[0]
 56             tiaojian_after = tiaojian[1]
 57             if tiaojian_before in rol_name and tiaojian_after.isdigit():
 58                 tiaojian_suoyin = rol_name.index(tiaojian_before)
 59                 with open(FileName, 'r', encoding='utf-8') as f:
 60                     for line in f:
 61                         line = line.strip().split(',')
 62                         if int(line[tiaojian_suoyin]) <= int(tiaojian_after):
 63                             if rol_suoyin == 5:
 64                                 res.append(line)
 65                             else:
 66                                 res.append(line[rol_suoyin])
 67                 return res
 68             else:
 69                 return '条件输入有误'
 70         elif '=' in tiaojian:
 71             tiaojian = tiaojian.split('=')
 72             tiaojian_before = tiaojian[0]
 73             tiaojian_after = tiaojian[1]
 74             if tiaojian_before in rol_name:
 75                 tiaojian_suoyin = rol_name.index(tiaojian_before)
 76                 with open(FileName, 'r', encoding='utf-8') as f:
 77                     for line in f:
 78                         line = line.strip().split(',')
 79                         if line[tiaojian_suoyin] == tiaojian_after:
 80                             if rol_suoyin == 5:
 81                                 res.append(line)
 82                             else:
 83                                 res.append(line[rol_suoyin])
 84                 return res
 85             else:
 86                 return '条件输入有误'
 87         elif '>' in tiaojian:
 88             tiaojian = tiaojian.split('>')
 89             tiaojian_before = tiaojian[0]
 90             tiaojian_after = tiaojian[1]
 91             if tiaojian_before in rol_name and tiaojian_after.isdigit():
 92                 tiaojian_suoyin = rol_name.index(tiaojian_before)
 93                 with open(FileName, 'r', encoding='utf-8') as f:
 94                     for line in f:
 95                         line = line.strip().split(',')
 96                         if int(line[tiaojian_suoyin]) > int(tiaojian_after):
 97                             if rol_suoyin == 5:
 98                                 res.append(line)
 99                             else:
100                                 res.append(line[rol_suoyin])
101                 return res
102             else:
103                 return '条件输入有误'
104         elif '<' in tiaojian:
105             tiaojian = tiaojian.split('<')
106             tiaojian_before = tiaojian[0]
107             tiaojian_after = tiaojian[1]
108             if tiaojian_before in rol_name and tiaojian_after.isdigit():
109                 tiaojian_suoyin = rol_name.index(tiaojian_before)
110                 with open(FileName, 'r', encoding='utf-8') as f:
111                     for line in f:
112                         line = line.strip().split(',')
113                         if int(line[tiaojian_suoyin]) < int(tiaojian_after):
114                             if rol_suoyin == 5:
115                                 res.append(line)
116                             else:
117                                 res.append(line[rol_suoyin])
118                 return res
119             else:
120                 return '条件输入有误'
121         else:
122             if rec[4] == 'like':
123                 if tiaojian in rol_name:
124                     tiaojian_suoyin = rol_name.index(tiaojian)
125                     tiaojian_after = rec[5]
126                     with open(FileName, 'r', encoding='utf-8') as f:
127                         for line in f:
128                             line = line.strip().split(',')
129                             if tiaojian_after in line[tiaojian_suoyin]:
130                                 if rol_suoyin == 5:
131                                     res.append(line)
132                                 else:
133                                     res.append(line[rol_suoyin])
134                     return res
135                 else:
136                     return '条件输入有误'
137             else:
138                 return '条件输入有误'
139     if rec[1] in rol_name:
140         return judge(rol_name.index(rec[1]))
141     elif ',' in rec[1]:
142         rol_douhao_list = rec[1].split(',')
143         rol_number = len(rol_douhao_list)
144         dic = {'id': 0, 'name': 0, 'age': 0, 'phone': 0, 'job': 0}
145         judge_list = []
146         tiaojian = rec[3]
147         for i in rol_douhao_list:
148             if i not in rol_name:
149                 return '列名输入有误'
150             dic[i] += 1
151         if rol_number <= 4:
152             if rol_number == 2:
153                 r1, r2 = rol_douhao_list[0], rol_douhao_list[1]
154                 if r1 == r2:
155                     return '列名重复'
156                 rol_suoyin1, rol_suoyin2 = rol_name.index(r1), rol_name.index(r2)
157                 if '>' in tiaojian:
158                     tiaojian = tiaojian.split('>')
159                     tiaojian_before = tiaojian[0]
160                     tiaojian_after = tiaojian[1]
161                     if tiaojian_before in rol_name and tiaojian_after.isdigit():
162                         tiaojian_suoyin = rol_name.index(tiaojian_before)
163                         with open(FileName,'r',encoding='utf-8') as f:
164                             for line in f:
165                                 line = line.strip().split(',')
166                                 if int(line[tiaojian_suoyin]) > int(tiaojian_after):
167                                     res.append([line[rol_suoyin1],line[rol_suoyin2]])
168                         return res
169                     else:
170                         return '条件输入有误'
171                 elif '<' in tiaojian:
172                     tiaojian = tiaojian.split('<')
173                     tiaojian_before = tiaojian[0]
174                     tiaojian_after = tiaojian[1]
175                     if tiaojian_before in rol_name and tiaojian_after.isdigit():
176                         tiaojian_suoyin = rol_name.index(tiaojian_before)
177                         with open(FileName,'r',encoding='utf-8') as f:
178                             for line in f:
179                                 line = line.strip().split(',')
180                                 if int(line[tiaojian_suoyin]) < int(tiaojian_after):
181                                     res.append([line[rol_suoyin1],line[rol_suoyin2]])
182                         return res
183                     else:
184                         return '条件输入有误'
185                 elif '=' in tiaojian:
186                     tiaojian = tiaojian.split('=')
187                     tiaojian_before = tiaojian[0]
188                     tiaojian_after = tiaojian[1]
189                     if tiaojian_before in rol_name:
190                         tiaojian_suoyin = rol_name.index(tiaojian_before)
191                         with open(FileName,'r',encoding='utf-8') as f:
192                             for line in f:
193                                 line = line.strip().split(',')
194                                 if line[tiaojian_suoyin] == tiaojian_after:
195                                     res.append([line[rol_suoyin1],line[rol_suoyin2]])
196                         return res
197                     else:
198                         return '条件输入有误'
199                 else:
200                     if rec[4] == 'like':
201                         if tiaojian in rol_name:
202                             tiaojian_suoyin = rol_name.index(tiaojian)
203                             tiaojian_after = rec[5]
204                             with open(FileName, 'r', encoding='utf-8') as f:
205                                 for line in f:
206                                     line = line.strip().split(',')
207                                     if tiaojian_after in line[tiaojian_suoyin]:
208                                         res.append([line[rol_suoyin1],line[rol_suoyin2]])
209                             return res
210                         else:
211                             return '条件输入有误'
212                     else:
213                         return '条件输入有误'
214             elif rol_number == 3:
215                 r1, r2, r3= rol_douhao_list[0], rol_douhao_list[1], rol_douhao_list[2]
216                 if r1 == r2 or r1 == r3 or r2 == r3:
217                     return '列名重复'
218                 rol_suoyin1, rol_suoyin2, rol_suoyin3 = rol_name.index(r1), rol_name.index(r2), rol_name.index(r3)
219                 if '>' in tiaojian:
220                     tiaojian = tiaojian.split('>')
221                     tiaojian_before = tiaojian[0]
222                     tiaojian_after = tiaojian[1]
223                     if tiaojian_before in rol_name and tiaojian_after.isdigit():
224                         tiaojian_suoyin = rol_name.index(tiaojian_before)
225                         with open(FileName, 'r', encoding='utf-8') as f:
226                             for line in f:
227                                 line = line.strip().split(',')
228                                 if int(line[tiaojian_suoyin]) > int(tiaojian_after):
229                                     res.append([line[rol_suoyin1], line[rol_suoyin2], line[rol_suoyin3]])
230                         return res
231                     else:
232                         return '条件输入有误'
233                 elif '<' in tiaojian:
234                     tiaojian = tiaojian.split('<')
235                     tiaojian_before = tiaojian[0]
236                     tiaojian_after = tiaojian[1]
237                     if tiaojian_before in rol_name and tiaojian_after.isdigit():
238                         tiaojian_suoyin = rol_name.index(tiaojian_before)
239                         with open(FileName, 'r', encoding='utf-8') as f:
240                             for line in f:
241                                 line = line.strip().split(',')
242                                 if int(line[tiaojian_suoyin]) < int(tiaojian_after):
243                                     res.append([line[rol_suoyin1],line[rol_suoyin2], line[rol_suoyin3]])
244                         return res
245                     else:
246                         return '条件输入有误'
247                 elif '=' in tiaojian:
248                     tiaojian = tiaojian.split('=')
249                     tiaojian_before = tiaojian[0]
250                     tiaojian_after = tiaojian[1]
251                     if tiaojian_before in rol_name:
252                         tiaojian_suoyin = rol_name.index(tiaojian_before)
253                         with open(FileName, 'r', encoding='utf-8') as f:
254                             for line in f:
255                                 line = line.strip().split(',')
256                                 if line[tiaojian_suoyin] == tiaojian_after:
257                                     res.append([line[rol_suoyin1],line[rol_suoyin2], line[rol_suoyin3]])
258                         return res
259                     else:
260                         return '条件输入有误'
261                 else:
262                     if rec[4] == 'like':
263                         if tiaojian in rol_name:
264                             tiaojian_suoyin = rol_name.index(tiaojian)
265                             tiaojian_after = rec[5]
266                             with open(FileName, 'r', encoding='utf-8') as f:
267                                 for line in f:
268                                     line = line.strip().split(',')
269                                     if tiaojian_after in line[tiaojian_suoyin]:
270                                         res.append([line[rol_suoyin1],line[rol_suoyin2], line[rol_suoyin3]])
271                             return res
272                         else:
273                             return '条件输入有误'
274                     else:
275                         return '条件输入有误'
276             elif rol_number == 4:
277                 r1, r2, r3, r4 = rol_douhao_list[0], rol_douhao_list[1], rol_douhao_list[2], rol_douhao_list[3]
278                 if r1 == r2 or r1 == r3 or r1 == r4 or r2 == r3 or r2 == r4 or r3 == r4:
279                     return '列名重复'
280                 rol_suoyin1, rol_suoyin2, rol_suoyin3, rol_suoyin4 = rol_name.index(r1), rol_name.index(r2), rol_name.index(r3), rol_name.index(r4)
281                 if '>' in tiaojian:
282                     tiaojian = tiaojian.split('>')
283                     tiaojian_before = tiaojian[0]
284                     tiaojian_after = tiaojian[1]
285                     if tiaojian_before in rol_name and tiaojian_after.isdigit():
286                         tiaojian_suoyin = rol_name.index(tiaojian_before)
287                         with open(FileName, 'r', encoding='utf-8') as f:
288                             for line in f:
289                                 line = line.strip().split(',')
290                                 if int(line[tiaojian_suoyin]) > int(tiaojian_after):
291                                     res.append([line[rol_suoyin1], line[rol_suoyin2], line[rol_suoyin3], line[rol_suoyin4]])
292                         return res
293                     else:
294                         return '条件输入有误'
295                 elif '<' in tiaojian:
296                     tiaojian = tiaojian.split('<')
297                     tiaojian_before = tiaojian[0]
298                     tiaojian_after = tiaojian[1]
299                     if tiaojian_before in rol_name and tiaojian_after.isdigit():
300                         tiaojian_suoyin = rol_name.index(tiaojian_before)
301                         with open(FileName, 'r', encoding='utf-8') as f:
302                             for line in f:
303                                 line = line.strip().split(',')
304                                 if int(line[tiaojian_suoyin]) < int(tiaojian_after):
305                                     res.append([line[rol_suoyin1],line[rol_suoyin2], line[rol_suoyin3], line[rol_suoyin4]])
306                         return res
307                     else:
308                         return '条件输入有误'
309                 elif '=' in tiaojian:
310                     tiaojian = tiaojian.split('=')
311                     tiaojian_before = tiaojian[0]
312                     tiaojian_after = tiaojian[1]
313                     if tiaojian_before in rol_name:
314                         tiaojian_suoyin = rol_name.index(tiaojian_before)
315                         with open(FileName, 'r', encoding='utf-8') as f:
316                             for line in f:
317                                 line = line.strip().split(',')
318                                 if line[tiaojian_suoyin] == tiaojian_after:
319                                     res.append([line[rol_suoyin1],line[rol_suoyin2], line[rol_suoyin3], line[rol_suoyin4]])
320                         return res
321                     else:
322                         return '条件输入有误'
323                 else:
324                     if rec[4] == 'like':
325                         if tiaojian in rol_name:
326                             tiaojian_suoyin = rol_name.index(tiaojian)
327                             tiaojian_after = rec[5]
328                             with open(FileName, 'r', encoding='utf-8') as f:
329                                 for line in f:
330                                     line = line.strip().split(',')
331                                     if tiaojian_after in line[tiaojian_suoyin]:
332                                         res.append([line[rol_suoyin1],line[rol_suoyin2], line[rol_suoyin3], line[rol_suoyin4]])
333                             return res
334                         else:
335                             return '条件输入有误'
336                     else:
337                         return '条件输入有误'
338         elif rol_number == 5:
339             for v in dic.values():
340                 judge_list.append(v)
341             if judge_list == [1, 1, 1, 1, 1]:
342                 return judge(5)
343             else:
344                 return '列名重复'
345         else:
346             return '列名输入有误'
347     else:
348         return '列名输入有误或语法错误'
349 
350 @login
351 def add_operate(rec):
352     '''
353     添加操作函数
354     接收处理后的列表
355     返回存放添加结果和所添加数据的列表
356     '''
357     if rec[1].isdigit() and rec[2].isdigit():
358         with open(FileName, 'a+', encoding='utf-8') as f:
359             f.seek(0)
360             for line in f:
361                 yg_id = str(int(line.strip().split(',')[0])+1)
362             # for line in f:
363             #     yg_id = line.strip().split(',')[0]
364                 # yg_add = yg_id + ',' + ','.join(rec)
365             f.write(f'\n{yg_id},{",".join(rec)}')
366         return f'成功添加数据:{yg_id},{",".join(rec)}'
367     else:
368         return '输入错误'
369 
370 @login
371 def del_operate(rec):
372     '''
373     删除操作函数
374     接收处理后的列表
375     返回存放删除结果和存放所删除结果的列表
376     '''
377     rec = ''.join(rec)
378     yg_dic = {}
379     del_line = []
380     def operate():
381         count = 0
382         re_count = 0
383         with open(FileName, encoding='utf-8') as f1,\
384             open(FileName+'.bak', 'w', encoding='utf-8') as f2:
385             read_lines = f1.readlines()
386             for line in read_lines:
387                 line = line.split(',')
388                 if rec not in line:
389                     if count < len(read_lines)-2:
390                         line = ','.join(line)
391                         f2.write(line)
392                         count += 1
393                         continue
394                     else:
395                         line = ','.join(line).strip()
396                         f2.write(line)
397                         continue
398                 del_line.append(','.join(line).strip())
399                 re_count += 1
400         if re_count == 1:
401             yorn = input(f'删除数据:\n\t{del_line}\n确认删除(Y/N):')
402             if yorn.lower() == 'y':
403                 os.remove(FileName)
404                 os.rename(FileName+'.bak', FileName)
405                 return f'删除数据成功:{del_line}'
406             else:
407                 return f'删除数据操作取消'
408         else:
409             os.remove(FileName+'.bak')
410             l = []
411             for i in range(len(del_line)):
412                 l.append(del_line[i]+'\n')
413             del_line_fail = '\t\t'.join(l).strip()
414             return f'ERROR:输入姓名重复(请尝试使用id删除)\n\t' \
415                    f'重复数据如下:\n\t\t{del_line_fail}'
416     with open(FileName, encoding='utf-8') as f:
417         for line in f:
418             line = line.strip().split(',')
419             yg_dic.setdefault(line[0], line[1])
420     if rec in yg_dic or rec in yg_dic.values():
421         return operate()
422     # elif rec in yg_dic.values():
423     #     return operate()
424     else:
425         return '输入错误'
426 
427 @login
428 def update_operate(rec):
429     '''
430     修改操作函数(仅对唯一对象进行修改)
431     接收处理后的列表
432     返回存放修改结果和所修改对象的列表
433     '''
434     rol_name = ['id', 'name', 'age', 'phone', 'job']
435     if '=' in rec[1]:
436         rol = rec[1].split('=')[0]
437         new_data = rec[1].split('=')[1]
438         tiaojian = rec[3]
439         read_lines_suoyin = 0
440         count = 0
441         if rol in rol_name:
442             rol_suoyin = rol_name.index(rol)
443             with open(FileName, encoding='utf-8') as f:
444                 read_lines = f.readlines()
445             if '=' in tiaojian:
446                 tiaojian_before = tiaojian.split('=')[0]
447                 if tiaojian_before in rol_name:
448                     tiaojian_before_suoyin = rol_name.index(tiaojian_before)
449                     tiaojian_after = tiaojian.split('=')[1]
450                     for i in read_lines:
451                         i = i.split(',')
452                         # print(tiaojian_after, i[tiaojian_before_suoyin],tiaojian_after == i[tiaojian_before_suoyin])
453                         # print(len(read_lines))
454                         # print("i :", i, read_lines[len(read_lines)-1].split(','))
455                         # print(i[tiaojian_before_suoyin] != tiaojian_after)
456                         if i[tiaojian_before_suoyin].strip() == tiaojian_after:
457                             count += 1
458                             i[rol_suoyin] = new_data
459                             updata_data = ','.join(i)
460                             read_lines[read_lines_suoyin] = updata_data
461                             if count > 1:
462                                 return '查询结果不唯一,无法修改'
463                         if i == read_lines[len(read_lines)-1].split(',') and count == 0\
464                                 and i[tiaojian_before_suoyin] != tiaojian_after:
465                             return '条件查询结果为空'
466                         read_lines_suoyin += 1
467                     with open(FileName+'.bak', 'w', encoding='utf-8')as f1:
468                         for i in read_lines:
469                             f1.write(f'{i}')
470                     os.remove(FileName)
471                     os.rename(FileName+'.bak', FileName)
472                     return f'成功修改数据:{updata_data.strip()}'
473                 else:
474                     return '输入有误或语法错误'
475             else:
476                 return '现仅支持条件为等于时的修改'
477         else:
478             return '输入有误或语法错误'
479     else:
480         return '语法错误'
481 
482 def first_operate(rec):
483     '''
484     初步处理函数
485     接收用户输入对象并分割为列表
486     返回将要执行的函数
487     '''
488     rec = rec.split()
489     if len(rec)>= 4 and ("'" in rec[3] or '"' in rec[3]):
490         rec[3] = rec[3].replace("'", "", 2)
491         rec[3] = rec[3].replace('"', '', 2)
492     if rec[0] == 'select' and len(rec) >= 4 and rec[2] == 'where':
493         return select_operate(rec)
494     elif rec[0] == 'set' and len(rec) >= 4 and 'where' in rec:
495         return update_operate(rec)
496     elif len(rec) == 1:
497         return del_operate(rec)
498     elif len(rec) == 4:
499         return add_operate(rec)
500     else:
501         return '输入错误'
502 
503 while 1:
504     if tip:
505         tip = False
506         print('输入help可查看各语句用法')
507     rec = input('>>>')
508     if rec.isspace():
509         print('输入错误')
510     elif rec == '':
511         continue
512     elif rec == 'exit' or rec == 'quit' or rec == '\q':
513         print('退出系统...')
514         break
515     elif rec == 'help' or rec == 'select -h' or rec == 'set -h':
516         if rec == 'help':
517             print('''    1.文件存储格式为:id,name,age,phone,job
518     2.增加:直接按序输入姓名 年龄 手机号 工作即可 以空格间隔
519     3.查询:select 列名1,列名2,... where 列名条件
520     4.删除指定员工,直接输入员工id或名字即可
521     5.修改:set 列名='新的值' where 条件''')
522         elif rec == 'select -h':
523             print('查询:select 列名1,列名2,... where 列名条件')
524         elif rec == 'set -h':
525             print('修改:set 列名="新的值" where 条件')
526     else:
527         res = first_operate(rec)
528         print('结果如下:')
529         if type(res) == list:
530             if '登入成功' in res:
531                 FLAG = True
532                 for i in res:
533                     if type(i) == list:
534                         for j in i:
535                             print(f'\t{j}')
536                     else:
537                         print(f'\t{i}')
538             elif res == []:
539                 print('\t未查询到结果')
540             else:
541                 for i in res:
542                     if type(i) == list:
543                         for j in i:
544                             print(f'\t{j}', end=' ')
545                         print('')
546                     else:
547                         print(f'\t{i}')
548         else:
549             print(f'\t{res}')
代码
以上~


这篇关于python实现员工信息表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程