员工信息表,增删查改程序
#!/usr/bin/env python# _*_ coding:UTF-8 _*_ import reimport os # 1.可进行模糊查询,语法至少支持下面3种查询语法:# find name,age from staff_table where age > 22 #找出年龄大于22岁的所有人,并返回 name和age信息# find * from staff_table where dept = "IT" #找出所有部门式IT的人的信息# find * from staff_table where enroll_date like "2013" # 模糊查询找出所有2013年入职的人# 2 添加员⼯工信息(⽤用户的⼿手机号不不允许重复) 添加员⼯工信息(⽤用户的⼿手机号不不允许重复)# add staff_table Mosson,18,13678789527,IT,2018-12-11# 3 删除员⼯工信息(根据序号删除相应员⼯工的信息) 删除员⼯工信息(根据序号删除相应员⼯工的信息)# del from staff_table where id = 10# 需求:从staff_table中删除序号id为10的这⼀一条员⼯工的信息# 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.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。 def make_dict_list(flie_name): '''将文件内容转化一下,列表中套嵌字典的格式''' with open(flie_name, 'r', encoding='utf-8') as read_f: li = [{'id': i.split(',')[0], 'name': i.split(',')[1], 'age': i.split(',')[2], 'phone': i.split(',')[3], 'dept': i.split(',')[4], 'enroll_date': i.split(',')[5]} for i in read_f] return li # 查找def find_staff_info(file_name, show_contents, match_rules): list_dic = make_dict_list(flie_name=file_name) new_staff_info = [] # if match_rules[0] == 'id' or match_rules[0] == 'age': # if match_rules[1] == '>': # 进行分割之后如果数量大于1, 那么判断的就是age>22的 # new_staff_info = [i for i in list_dic if int(i[match_rules[0]]) > int(match_rules[2])] # if match_rules[1] == '<': # new_staff_info = [i for i in list_dic if int(i[match_rules[0]]) < int(match_rules[2])] # if match_rules[1] == '=': # new_staff_info = [i for i in list_dic if int(i[match_rules[0]]) == int(match_rules[2])] # for i in new_staff_info: # for j in show_contents: # print(j, i[j], end='\t') # print('') if match_rules[0] == 'id' or match_rules[0] == 'age': if match_rules[1] == '>': for i in list_dic: if int(i[match_rules[0]]) > int(match_rules[2]): new_staff_info.append(i) if match_rules[1] == '<': for i in list_dic: if int(i[match_rules[0]]) < int(match_rules[2]): new_staff_info.append(i) if match_rules[1] == '=': for i in list_dic: if int(i[match_rules[0]]) == int(match_rules[2]): new_staff_info.append(i) # if match_rules[0] == 'dept': # for i in list_dic: # if i[match_rules[0]] == match_rules[2]: # new_staff_info.append(i) # if match_rules[0] == 'enroll_date': # for i in list_dic: # if i[match_rules[0]].startswith(match_rules[2]): # new_staff_info.append(i) if match_rules[0] in list_dic[1]: for i in list_dic: if match_rules[2] in i[match_rules[0]]: new_staff_info.append(i) count = 0 for i in new_staff_info: for j in show_contents: print(j, i[j].strip(), end='\t') print('') count += 1 print('查找到了%d条数据' % count) return new_staff_info #增加def add_staff_info(file_name, staff_info): num = 0 # 文件最后一行的员工id 这时还时一个字符串的数字 with open(file_name, 'rb',) as read_f: for line in read_f: offs = -30 # 设定偏移量,倒向查找所以为 负数 while True: read_f.seek(offs, 2) # 2 为文件末尾开始查找的意思,偏移量倒移到-30的位置 date = read_f.readlines() # 将光标当前位置之后的内容 读取到列表中,直到遇到下一个\n 换行符时,算作另一个列表元素 if len(date) > 1: # 判断date元素个数,超过1了。那么最后一个元素就是,文件的最后一行内容 num = date[-1].decode('utf-8').split(',')[0] # 获取文件最后一行内容的第一个数字是多少 break else: offs *= 2 with open(file_name, 'r', encoding='utf-8') as f, open(file_name, 'a', encoding='utf-8') as write_f: count = 0 for line_info in f: if staff_info.split(',')[2] in line_info: # 判断电话是否重复 print('电话不允许重复') break # 重复了就退出循环,后续代码不执行。 else: new_staff_info = '\n%d,%s' % (int(num) + 1, staff_info) # 将数字+1 再和 传进来的列表进行拼接成一个文件内容 write_f.write(new_staff_info) # 写入内容 count += 1 print('增加了%d个员工'%count) # 删除def del_staff_info(file_name, match_rules): count = 1 # 员工ID每次加+1 count_ont = 0 flag = False with open(file_name, 'r', encoding='utf-8') as read_f, open('new_file', 'w', encoding='utf-8') as write_f: for line in read_f: staff_info_list = line.split(',', maxsplit=1) if int(staff_info_list[0]) == int(match_rules[2]): count_ont += 1 continue else: if not flag: write_f.write('%d,%s' % (count, staff_info_list[1].strip())) flag = True else: write_f.write('\n%d,%s' % (count, staff_info_list[1].strip())) count += 1 # 员工ID每次加+1 print('删除了%d个员工信息' % count_ont) os.rename(file_name, 'new_file.bak') os.rename('new_file', file_name) os.remove('new_file.bak') #更新def update_staff_info(file_name, old_info, new_info): list_dic = make_dict_list(flie_name=file_name) count = 0 for dic in list_dic: if dic[old_info[0]] == old_info[2] and dic[new_info[0]] != new_info[2]: dic[new_info[0]] = new_info[2] count += 1 print('更换了%d'%count) with open(file_name, 'w', encoding='utf-8') as write_f: for i in list_dic: staff_info_list = [] for j in i.values(): staff_info_list.append(j) write_f.write(','.join(staff_info_list)) '''find name,age from function_homework where age > 22 find * from function_homework where dept = ITfind * from function_homework where enroll_date like 2013 add function_homework Mosson,18,13678789527,IT,2018-12-11 del from function_homework where id = 10 update function_homework set dept = Market where dept = ITupdate function_homework set age = '25' where name = "Alex Li"'''# 判断需求def judge_demand(command): if command.startswith('find'): file_name = re.findall('(?<=from).+(?=where)', command)[0].strip() # 不论什么操作都需要统一的文件名 ret = command if '*' in command: # 把带 * 号的.全部替换掉. ret = command.replace('*', 'id,name,age,phone,dept,enroll_date', 1) show_contents = re.findall('(?<=find).+(?=from)', ret)[0].strip().split(',') # 需要显示哪些信息.不需要过多处理 match_rules = re.findall('(?<=where).+', ret)[0].split(maxsplit=2) # 判断信息 find_staff_info(file_name, show_contents, match_rules) if command.startswith('add'): add_info_list = command.split(' ') # 使用空格分割字符串 add_file = add_info_list[1] add_info = add_info_list[2] add_staff_info(add_file, add_info) if command.startswith('del'): file_name = re.findall('(?<=from).+(?=where)', command)[0].strip() match_rules = re.findall('(?<=where).+', command)[0].split(maxsplit=2) del_staff_info(file_name, match_rules) if command.startswith('update'): file_name = re.findall('(?<=update).+(?=set)', command)[0].strip() new_info = re.findall('(?<=set).+(?=where)', command)[0].strip().split(maxsplit=2) old_info = re.findall('(?<=where).+', command)[0].strip().split(maxsplit=2) update_staff_info(file_name, old_info, new_info) if __name__ == '__main__': command_str = input('请输入命令,除显示需求外,其余每个字符之间加空格:').replace('"', '') new_command_str = command_str.replace('\'', '') # 把输入进来的字符串中的 ' " 全部替换 judge_demand(new_command_str) # 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# 11,Mosson,18,13188888888,IT,2018-12-11
转载于:https://www.cnblogs.com/chengege/p/10232158.html
员工信息表,增删查改程序相关推荐
- JAVA原生mvc实现用户信息的增删查改
笔者最近学完jsp和servlet,于是心血来潮的打算写个简单的用户案例 环境准备: 开发工具eclipse jdk-1.8.0_72 tomcat-9.0.5 前端部分: 1.自己手写了一套样式 2 ...
- JAVA学生信息管理系统——增删查改
1.首先添加学生类 2.再实现增删查改 在编程中遇到的小问题: 1.case语句中代码重复的话,可以注释掉,程序依然会向下执行 2.编写查询时,要考虑没有学生的情况 3.编写增加和修改和查询时,要以唯 ...
- C语言实现顺序表(增删查改等数据管理)
顺序表seqlist 小伙伴们,提到顺序表会想到什么呢? 数组?还是链表? 其实,数组和链表都是属于线性表,而链表在逻辑上是线性的,并非物理存储上也为线性:而数组无论在逻辑上还是物理存储上均为线性 所 ...
- 海强集团的多表增删查改
多表的增删查改 创建数据库和表并且插入数据 添加外键 插入数据 数据库完成 然后创建ASP.NET 并且搭建三层架构 在Model层里添加ADO.NET实体数据模型 并且添加所有引用 不知道的可以看上 ...
- 数据结构C语言实现顺序表——增删查改操作实现详解
顺序表 顺序表是什么? 顺序表是将元素顺序地存放在一块连续的存储区里,元素间的顺序关系由它们的存储顺序自然表示.实现增删查改的功能. 顺序表所需的头文件: #include<stdio.h> ...
- java顺序表增删查改_Java实现顺序表的增删改查
public class MyArrayList { private int[] array; //代表的是存在数据的数组 //array.length代表的是数组的容量 private int ...
- ASP.NET 电影票信息的增删查改
题目 1.使用Code First技术创建一个Movie数据模型. public class Movie{public int ID { get; set; } //电影编号public string ...
- 初学jsp课,一个基于jsp+javabean+servlet+sql server小型房源网站,实现了用户表,房源表,及留言板的增删查改。使用deamwear编译器
1 设计目的 <Web应用开发课程设计>是实践性教学环节之一,是<Web程序设计>课程的辅助教学课程.通过课程设计,使学生掌握Web网站的基本概念,结合实际的操作和设计,巩固课 ...
- 【超详细Django网站开发过程7】便利店管理系统之——利用管理员用户对客户数据进行增删查改
提到数据库,就免不了对信息的增删查改,今天来做一个管理员用户对顾客数据的增删查改功能. 导航 1.增删查改中的--"查"操作如下: 2.增删查改中的--"增"操 ...
最新文章
- 湖南工业大学java试卷_湖南工业大学数控加工技术试卷.doc
- 公钥私钥 多久过期_上传到公钥服务器的gpg公钥过期了会被删除吗?
- Java设计模式透析之 —— 策略(Strategy)
- boost::graph模块BC聚类算法程序的实现
- docker镜像了解(建立私有仓库,基于已有镜像创建,Docker的数据管理) 基于Dockerfile创建,基于本地模板创建,端口映射,容器互联)
- IPSEC非单播流量处理
- C# dynamic 类型用法举例
- python创建虚拟环境venv_Python 3 使用venv创建虚拟环境
- 微信公众号开发 ssl connect error
- [小技巧]快速生成验证码
- 7-135 二叉搜索树的2层结点统计
- Codeforces Round #483 (Div. 1) A. Finite or not?
- 调研AutoGluon数据处理与Tabular-NN
- windows11恢复ie浏览器的方法教程
- [读书笔记]《刻意学习》
- svn多分支开发合并技巧(idea or tortoiseSVN)
- 用p5.js绘制创意自画像
- HTTP POST 参数格式
- linux基本权限例子,Linux-3 文件权限-基本权限
- PyCenterNetDetector is not in the models registry