python创建员工_python作业员工信息表程序(第四周)
#!/usr/bin/env python#-*- coding:utf-8 -*-#Author: Colin Yao
"""python 员工信息表操作"""
importsysimportosdefselect1():'''查看文件函数
:return:'''with open('peopledb', 'r', encoding='utf-8') as f:
line=f.readlines()for i inline:print(i)defselect():'''查询函数
:return:'''msg= '''请输入或复制查询命令例如:
1. select name,age from staff_table where age > 22
2. select * from staff_table where dept = "IT"
3. select * from staff_table where enroll_date like "2013"'''
print(msg)
user_choice_input= input(">>>:")
user_choice_input1= user_choice_input.split(' ')if user_choice_input == 'select name,age from staff_table where age > %s' % (user_choice_input1[7]):
with open('peopledb', 'r+', encoding='utf-8') as f:
list1=[]
count=0for line inf:
i= line.strip().split(',')if i[2] > user_choice_input1[7]:
list1.append(i)for s inlist1:
count= count + 1
for j inlist1:print(j)print('满足条件的个数为>>:%s' %(count))elif user_choice_input == ('select * from staff_table where dept = %s' % (user_choice_input1[7])):
with open('peopledb', 'r+', encoding='utf-8') as f:
list2=[]
count=0for line inf:
i1= line.strip().split(',')if i1[4] == eval(user_choice_input1[7]):
list2.append(i1)for s1 inlist2:
count= count + 1
#print(list1)
for j1 inlist2:print(j1)print('满足条件的个数为>>:%s' %(count))elif user_choice_input == ('select * from staff_table where enroll_date like %s' % (user_choice_input1[7])):
with open('peopledb', 'r+', encoding='utf-8') as f:
list3=[]
list4=[]
count=0for line inf:
i= line.strip().split(',')
list3.append(i)for j inlist3:
m= j[5].split('-')if m[0] == eval(user_choice_input1[7]):
list4.append(j)for s inlist4:
count= count + 1
if count < 1:print("没有找到类似的条目:")pass
else:pass
for j inlist4:print(j)print('满足条件的条数为>>:%s' %(count))return()defalter():'''添加函数
:return:'''msg= '''1)添加命令如下:Jack Wang,30,13304320533,HR,2015-05-03'''
print(msg)
user_choice_input= input("请输入命令>>>:")
user_choice_input1= user_choice_input.split(',')
with open('peopledb', 'r+', encoding='utf-8') as f:
list=[]for line inf:
s2= line.strip().split(',')
m= s2[3]
list.append(m)if user_choice_input1[2] inlist:print('这条记录已经存在')
main()else:
my_index= str(len(list) + 1)
user_choice_input1.insert(0, my_index)
user_choice_input1= ','.join(user_choice_input1)
f.write('\n')
f.write(user_choice_input1)
f.close()print("记录添加完成", '\n')
select1()return()defdelect():'''删除函数
:return:'''
print("请输入删除命令例如: 输入用户ID 既可以从staff_table里删除")
msg= '''1)按1 删除、直接删除ID即可
2)按2或者q退出
3)按任意返回上一层'''
print(msg)
user_choice_input= input("请输入命令>>>:")if user_choice_input == '1':print("现有的用户为:")
select1()print('\n')
user_choice_input1= input("请输入需要删除的用户ID:")
user_choice_input2=user_choice_input1[0]
f= open('peopledb', 'r+', encoding='utf-8')
f1= open('new_peopledb', 'w+', encoding='utf-8')for line inf:
i= line.strip().split(',')
i1=i[0]if user_choice_input2 !=i1:
i= ','.join(i)
f1.write(i)
f1.write('\n')else:continuef.close()
f1.close()
os.remove('peopledb')
os.rename('new_peopledb', 'peopledb')print('\n')
select1()elif user_choice_input == '2' or 'q':
sys.exit()return()defupdate():'''更新函数
:return:'''msg= '''1)这里第一个等号按照没有空格的格式划分
2)命令范例:UPDATE staff_table SET dept="INS" where dept = "HR"'''
print(msg)
user_choice_input= input("请输入命令>>>:")
user_choice_input1= user_choice_input.split(' ')
dept= user_choice_input1[3].split('=')
dept_new= dept[1]
dept_old= user_choice_input1[7]if user_choice_input == ('UPDATE staff_table SET dept=%s where dept = %s' %(dept_new, dept_old)):
dept_new1=eval(dept_new)
dept_old1=eval(dept_old)
f= open('peopledb', 'r+', encoding='utf-8')
f1= open('new_peopledb', 'w+', encoding='utf-8')for line inf:
i= line.strip().split(',')
dept_change= i[4]if dept_change ==dept_old1:
i[4] =eval(dept_new)
i= ','.join(i)
f1.write(i)
f1.write('\n')
f.close()
f1.close()
os.remove('peopledb')
os.rename('new_peopledb', 'peopledb')print('\n')
select1()pass
return()defmain():'''交互程序
:return:'''
print("员工信息表操作作业练习")
msg= '''1)查询
2)添加
3)删除
4)更新
5) 退出'''exit_flag=Falsewhile notexit_flag:print(msg)
user_choice= input("请选择>>:")if user_choice == '1':
select()elif user_choice == '2':
alter()elif user_choice == '3':
delect()elif user_choice == '4':
update()elif user_choice == '5' or 'q':
sys.exit()else:print("您的选择有误、请重新输入")
main()
python创建员工_python作业员工信息表程序(第四周)相关推荐
- JavaWeb第九次:程序设计题:在MySQL,创建一个学生信息数据库Student,创建一张学生注册信息表message,包含name (姓名)、age (年龄)、sex (性别)、educ
程序设计题: 在MySQL,创建一个学生信息数据库Student,创建一张学生注册信息表message,包含name (姓名).age (年龄).sex (性别).education(学历).phon ...
- python创建变量_python创建变量
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 创建: 当python变量'赋值'时,根据值类型创建变量,如:a=1引用: 当参 ...
- python创建列表_python创建与遍历List二维列表的方法
python 创建List二维列表 lists = [[] for i in range(3)] # 创建的是多行三列的二维列表 for i in range(3): lists[0].append( ...
- python 计算时间重叠_Python基于时间信息(即时、间隔)计算项目之间的相似性...
我想根据时间信息计算项目(0,1,2,3-.)之间的相似性.时间信息可以是时间即时(startdate).时间间隔(startdate.enddate)或null(NaT):请参阅下面的datafra ...
- python 创建子类_python创建子类的方法分析
本文实例讲述了python创建子类的方法.分享给大家供大家参考,具体如下: 如果你的类没有从任何祖先类派生,可以使用object作为父类的名字.经典类的声明唯一不同之处在于其没有从祖先类派生---此时 ...
- python创建追加_Python文件的创建与追加
(转载)https://www.xshell.net/python/python_open.html Posted by 破冰 on 2013-9-17 17:29 Tuesday 一.用Python ...
- python创建矩阵_Python创建对称矩阵的方法示例【基于numpy模块】
本文实例讲述了Python创建对称矩阵的方法.分享给大家供大家参考,具体如下: 对称(实对称)矩阵也即: step 1:创建一个方阵 >>> import numpy as np & ...
- 如何用python创建文件_python在指定目录创建文件
Python无法创建文件夹,如何制定创建目录?温柔也好,凶狠也罢.想有一个陌生的自己,给自己一些惊喜. /主目录/桌面/gnssIR_python-master:/usr/local/bin:/usr ...
- 用python创建你自己的命令行地址簿程序
题目来自书籍:Python3简明教程 题目描述:创建你自己的命令行地址簿程序.在这个程序中,你可以添加.修改.删除和搜索你的联系人(朋友.家人和同事等等)以及它们的信息(诸如电子邮件地址和/或电话号码 ...
最新文章
- [MySql]锁表与解锁
- linux shell概述,Linux学习 -- Shell基础 -- 概述
- CM005-逆向分析过程(上篇)
- 英特尔核显自定义分辨率_核显性能大提升 11代酷睿核显畅玩《战地5》
- 《高效程序员的45个习惯》-之二
- 新手博客,开博立言_Youcans2021
- 安装部署gitlab ci
- 信息学奥赛一本通(1195:判断整除)
- 【数字图像处理】求图像rice.png中米粒个数
- T-SQL远程数据库备份还原
- 深度:余额宝技术架构及演进
- 基于Java+SpringBoot+vue+node.js的智能农场管理系统详细设计和实现
- 新浪微博批量删除微博的方法
- 照片幻灯片java_Java的POI向幻灯片中插入图片算法设计
- iOS PDF 添加图片
- Windows11显示网速
- mysql逻辑模型的概念_概念模型、逻辑模型、物理模型区别?
- 房产java_Java学员作品-房地产项目
- api 接口响应数据格式有哪些
- 内连接、外连接、全外连接、交叉连接用法汇总(个人记录使用)
热门文章
- Python不能做游戏?游戏实战之-----《ink spill》(附游戏完整源码)
- python函数代码,这个python函数代码如何工作?
- html中超链接无效怎么办,超链接问题:“引用无效”怎么回事
- linux下查询汇编指令手册,Linux命令大全
- linux 常用压缩格式,Linux常见压缩格式之压缩与解压
- linux赋高权命令有哪些,Linux命令中的Z原来是这个意思
- C语言中怎么自动生成时间,在C语言中转换时间的基本方法介绍
- Openresty支持HTTP2
- 分布式版本控制工具 Git
- 3.第九章 树--9.3 二叉树结点类表示