作业需求:

1. 员工信息表程序,实现增删改查操作:

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

  select name,age from staff_table where age > 22

  select * from staff_table where dept = "IT"

  select * from staff_table where enroll_date like "2013"

  查到的信息,打印后,最后面还要显示查到的条数

3. 可创建新员工纪录,以phone做唯一键,staff_id需自增

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

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

  UPDATE staff_table SET dept="Market" where dept = "IT"

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

思路解析:

1. 将原始员工信息记录到文件

2. 读取文件并添加到列表,进行添加删除更新操作,并写入到文件中

程序核心代码:

peopledb

1,Jack Wang,30,43304320533,INS,2015-05-03

2,Alex Li,22,23651054608,IT,2013-04-01

员工信息表.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
"""python 员工信息表操作"""
import sys
import os def select1():
'''
查看文件函数
:return:
'''
with open('peopledb', 'r', encoding='utf-8') as f:
line = f.readlines()
for i in line:
print(i)
def select():
'''
查询函数
: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 = 0
for line in f:
i = line.strip().split(',')
if i[2] > user_choice_input1[7]:
list1.append(i)
for s in list1:
count = count + 1
for j in list1:
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 = 0
for line in f:
i1 = line.strip().split(',')
if i1[4] == eval(user_choice_input1[7]):
list2.append(i1)
for s1 in list2:
count = count + 1
# print(list1)
for j1 in list2:
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 = 0
for line in f:
i = line.strip().split(',')
list3.append(i)
for j in list3:
m = j[5].split('-')
if m[0] == eval(user_choice_input1[7]):
list4.append(j)
for s in list4:
count = count + 1
if count < 1:
print("没有找到类似的条目:")
pass
else:
pass
for j in list4:
print(j)
print('满足条件的条数为>>:%s' % (count))
return () def alter():
'''
添加函数
: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 in f:
s2 = line.strip().split(',')
m = s2[3]
list.append(m)
if user_choice_input1[2] in list:
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 () def delect():
'''
删除函数
:return:
'''
print("请输入删除命令例如: 输入用户ID 既可以从staff_table里删除")
msg = '''
1)按1 删除、直接删除ID即可
2)按2或者q退出
3)按任意返回上一层
'''
print(msg)
user_choice_input = input("请输入命令>>>: ")
if user_choice_input == '':
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 in f:
i = line.strip().split(',')
i1 = i[0]
if user_choice_input2 != i1:
i = ','.join(i)
f1.write(i)
f1.write('\n')
else:
continue
f.close()
f1.close()
os.remove('peopledb')
os.rename('new_peopledb', 'peopledb')
print('\n')
select1()
elif user_choice_input == '' or 'q':
sys.exit()
return () def update():
'''
更新函数
: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 in f:
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 () def main():
'''
交互程序
:return:
'''
print("员工信息表操作作业练习")
msg = '''
1)查询
2)添加
3)删除
4)更新
5) 退出
'''
exit_flag = False
while not exit_flag:
print(msg)
user_choice = input("请选择>>:")
if user_choice == '':
select()
elif user_choice == '':
alter()
elif user_choice == '':
delect()
elif user_choice == '':
update()
elif user_choice == '' or 'q':
sys.exit()
else:
print("您的选择有误、请重新输入") main()

python作业员工信息表程序(第四周)的更多相关文章

  1. day12 python作业:员工信息表

    作业要求: 周末大作业:实现员工信息表文件存储格式如下:id,name,age,phone,job1,Alex,22,13651054608,IT2,Egon,23,13304320533,Tearc ...

  2. python练习题-员工信息表

    周末大作业:实现员工信息表文件存储格式如下:id,name,age,phone,job1,Alex,22,13651054608,IT2,Egon,23,13304320533,Tearcher3,n ...

  3. python写员工信息表作业笔记

    需求 流程图

  4. python-查询员工信息表

    python查询员工信息表 基本要求: 用户可以模糊查询员工信息 显示匹配了多少条,匹配字符需要高亮显示 代码: #!/usr/env python #coding:utf-8 import time ...

  5. python基础之员工信息表作业

    周末大礼包 文件存储格式如下: id, name, age, phone, job 1, Alex, 22, 13651054608, IT 2, Egon, 23, 13304320533, Tea ...

  6. s9.16作业,员工信息表

    转载https://blog.csdn.net/qq_35883464/article/details/83151464 实现员工信息表文件存储格式如下:id,name,age,phone,job1, ...

  7. python's sixteenth day for me 员工信息表

    import os user_dic = { 'username':None, 'password':None, 'login':True } flag = False name_list = ['i ...

  8. python-打印简单公司员工信息表

    python-打印简单公司员工信息表 要求,输入name不为空,输入次数最多3次,3次后跳出程序: 知识点: raw_input str转int whil if elif else continue ...

  9. python3 员工信息表

    这是最后一条NLP了......来吧 十二,动机和情绪总不会错,只是行为没有效果而已 动机在潜意识里,总是正面的.潜意识从来不会伤害自己,只会误会的以为某行为可以满足该动机,而又不知道有其他做法的可能 ...

随机推荐

  1. HDU4803_Poor Warehouse Keeper

    题目很有意思,我想说其实我在比赛的时候就看过了一下这个题目,今天才这么快搞出来吧. 其实总共按上键的次数不会超过10个,我们可以每次假设相隔按两次上键之间按了xi次下键,由于上键的次数是确定的,所以最 ...

  2. BZOJ 2152 聪聪可可(树形DP)

    给出一颗n个点带边权的树(n<=20000),求随机选择两个点,使得它们之间的路径边权是3的倍数的概率是多少. 首先总的对数是n*n,那么只需要统计路径边权是3的倍数的点对数量就行了. 考虑将无 ...

  3. Linux学习笔记二:Ubuntu安装SSH(Secure Shell)服务

    Ubuntu默认是没有安装SSH(Secure Shell)服务,如果想要通过ssh链接到Ubuntu,我们需要手动安装ssh-server. SSH分客户端ssh-client,服务端ssh-ser ...

  4. Codeforces709

    A Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Kolya will p ...

  5. 【BZOJ2151】种树(贪心)

    [BZOJ2151]种树(贪心) 题面 BZOJ 题解 如果没有相邻不能选的限制,那么这就是一道傻逼题. 只需要用一个堆维护一下就好了. 现在加上了相邻点的限制,那么我们就对于当前位置加入一个撤销操作 ...

  6. 【POJ1741】Tree(点分治)

    [POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...

  7. 【BZOJ3894】文理分科(最小割)

    [BZOJ3894]文理分科(最小割) 题面 BZOJ Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过) 小P所在的班级要进行文理分科.他的班级可以用一个 ...

  8. 转载乙醇大师的appium简明教程

    appium简明教程(11)——使用resource id定位(仅支持安卓4.3以上系统) 乙醇 2014-06-28 21:01 阅读:16406 评论:21 appium简明教程(10)——控件定 ...

  9. bzoj2006: [NOI2010]超级钢琴(堆+RMQ)

    和上一道题同类型...都是用堆求第k大 考虑对于每一个r,怎么求出一个最优的l.显然只需要求出前缀和,用RMQ查询前面最小的l的前缀和就好了.但是对于一个r,每个l只能选一次,选了一次之后,考虑怎么把 ...

  10. 【learning】加权拟阵与贪心

    首先.. 这篇东西的话算是一个关于拟阵部分知识的小总结,有些语言相对来说偏向便于理解方面,所以可能..有一些说法会不是那么严谨大概是这样 ​ 一些概念 线性无关:一组数据中没有一个量可以写成其余量的线 ...