python 学习分享-实战篇增删改查作业
一大波函数来袭
作业要求:

1本次作业通过空格及逗号,将文件拆分成列表,在通过判断add、del、update、select等关键字,来判断用户执行的是哪种命令,根据不同的命令调用不同的函数去处理。
2增加操作及删除操作较为简单,根据规范来执行即可,查询及修改操作较为复杂。
3查询函数,首先要进行判断=及like是否在其中,根据这一点来确定select方式,然后判断关键字的位置,即其在列表中的位置。通过这些就可以查询出内容。
4修改函数类似于查询函数,因为修改操作比较单一,所以只需要判断关键字即可。
5最后就是主程序函数的编写,根据关键字判断是哪种操作,然后执行即可。
import time
staff_head = ['staff_id','name','age','phone','dept','enroll_date']
with open('user.txt','r') as user:
staff_table = []
for us_line in user:
sta_line = us_line.strip('\n').split(',')
staff_table.append(sta_line) #staff_table 列表
def staff_table_get(): #打印staff_table函数
print('STAFF_TABLE'.center(60,'='))
print('\033[34;0m【staff_id】【name】【age】【phone】【dept】【enroll_date】\033[0m')
for i in staff_table:
print(i)
def input_get(): #input order函数
print('ORDER'.center(60,'='))
add_order = 'add name=\033[1;31;0mLaay\033[0m age=\033[1;31;0m18\033[0m phone=\033[1;31;0m13563630129\033[0m dept=\033[1;31;0mIT\033[0m'
del_order = 'del \033[1;31;0m1\033[0m'
upd_order = 'update staff_table set \033[1;31;0mdept\033[0m = Market where dept = \033[1;31;0mIT\033[0m'
sel_order_1 = 'select \033[1;31;0mname,age\033[0m from staff_table where \033[1;31;0mage > 22\033[0m'
sel_order_2 = 'select \033[1;31;0m*\033[0m from staff_table where \033[1;31;0mdept = IT\033[0m'
sel_order_3 = 'select \033[1;31;0m*\033[0m from staff_table where \033[1;31;0menroll_date like 2013\033[0m'
print('增加=> ',add_order)
print('删除=> ',del_order)
print('修改=> ',upd_order)
print('查询=> ',sel_order_1)
print('查询=> ',sel_order_2)
print('查询=> ',sel_order_3)
print('退出=> ','\033[1;32;0mq\033[0m')
print('INPUT ORDER'.center(60, '='))
input_order = input('\033[31;0m Input your order:\033[0m' )
return input_order
def func_sel(input_ord): #查询函数
input_list = input_ord.split()
judge = ' '.join(input_list[5:])
out_list = []
for i in staff_table:
staff_id = i[0]
name = i[1]
age = int(i[2])
phone = int(i[3])
dept = i[4]
enroll_date = i[5]
if 'like' in judge:
if input_list[7] in i[staff_head.index(input_list[5])] and input_list[1] == '*':
out_list.append(i)
elif input_list[7] in i[staff_head.index(input_list[5])] and input_list[1] != '*':
input_list_mix = input_list[1].split(',')
out_mix = []
for j in input_list_mix:
out_mix.append(i[staff_head.index(j)])
out_list.append(out_mix)
elif '=' in judge:
if input_list[7] == i[staff_head.index(input_list[5])] and input_list[1] == '*':
out_list.append(i)
elif input_list[7] == i[staff_head.index(input_list[5])]and input_list[1] != '*':
input_list_mix = input_list[1].split(',')
out_mix = []
for j in input_list_mix:
out_mix.append(i[staff_head.index(j)])
out_list.append(out_mix)
else:
if eval(judge) and input_list[1] == '*':
out_list.append(i)
elif eval(judge) and input_list[1] != '*':
input_list_mix = input_list[1].split(',')
out_mix = []
for j in input_list_mix:
out_mix.append(i[staff_head.index(j)])
out_list.append(out_mix)
if len(out_list)>0:
print('查询结果'.center(60,'='))
for z in out_list:
print('\033[36;3m%s\033[3m'%(z))
print('共计有\033[35;3m{}\033[3m条数据'.format(len(out_list)))
else:
print('wrong ,please try again!')
def func_upd(input_ord): #更改函数
input_list = input_ord.split()
if input_list[3] in staff_head:
j = staff_head.index(input_list[3])
i_j = []
for i in staff_table:
i_j.append(i[j])
if input_list[9] in i_j:
for z in staff_table:
if z[j] == input_list[9]:
z[j] = input_list[5]
print('修改成功')
else:
continue
else:
print('wrong input ,please try again!')
else:
print('wrong input,please try again!')
#add name=bb age=22 phone=13563636541 dept=Sales
def func_add(input_ord): #增加函数
input_list = input_ord.split()
i_j = []
for i in staff_table:
i_j.append(i[3])
if input_list[3].split('=')[1] not in i_j:
staff_add = []
# staff_add[0] = str(len(staff_table)+2)
# staff_add[1] = input_list[1].split('=')[1]
# staff_add[2] = str(input_list[2].split('=')[1])
# staff_add[3] = str(input_list[3].split('=')[1])
# staff_add[4] = input_list[4].split('=')[1]
# staff_add[5] = str(time.strftime('%Y-%m-%d',time.localtime(time.time())))
staff_add.append(str(int(staff_table[-1][0])+1))
staff_add.append(input_list[1].split('=')[1])
staff_add.append(str(input_list[2].split('=')[1]))
staff_add.append(str(input_list[3].split('=')[1]))
staff_add.append(input_list[4].split('=')[1])
staff_add.append(str(time.strftime('%Y-%m-%d',time.localtime(time.time()))))
staff_table.append(staff_add)
print('增加成功')
else:
print('这个号码已经在表中了')
def func_del(input_ord):
input_list = input_ord.split()
i_j = []
for i in staff_table:
i_j.append(i[0])
if input_list[1] in i_j:
for j in staff_table:
if input_list[1] == j[0]:
staff_table.remove(j)
print('\033[34;0m删除成功\33[0m')
else:
continue
else:
print('wrong staff_id')
def judge_get(): #输入判断函数
while 1:
staff_table_get()
input_order = input_get()
if 'select' in input_order and len(input_order.split()) == 8:
func_sel(input_order)
elif 'update' in input_order and len(input_order.split()) == 10:
func_upd(input_order)
elif 'add' in input_order and len(input_order.split()) == 5:
func_add(input_order)
elif 'del' in input_order and len(input_order.split()) == 2:
func_del(input_order)
elif input_order.strip() == 'q':
break
else:
print('Wrong input,please try again')
continue
def wrt_op():
with open('user.txt','r') as old_us , open('user_back.txt','w') as back_user:
for l_b in old_us:
back_user.write(l_b)
with open('user.txt', 'w') as new_us :
for n_b in staff_table:
wrt = n_b[0]+','+n_b[1]+','+n_b[2]+','+n_b[3]+','+n_b[4]+','+n_b[5]+'\n'
new_us.write(wrt)
judge_get()
wrt_op()
python 学习分享-实战篇增删改查作业的更多相关文章
- python学习之-成员信息增删改查
python学习之-成员信息增删改查 主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env p ...
- pymongo学习第1篇——增删改查
参考文档: 1.https://docs.mongodb.org/getting-started/python/ 2.http://api.mongodb.org/python/current/api ...
- python学习之成员信息增删改查
主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env python# coding=utf8# ...
- python 学习分享-实战篇高级的ftp
#server代码 import socketserver,os,hashlib Base_paht = os.path.dirname(os.path.dirname(os.path.abspath ...
- python 学习分享-实战篇选课系统
# 角色:学校.学员.课程.讲师 # 要求: # 1. 创建北京.上海 2 所学校 # 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 # ...
- python 学习分享-实战篇类 Fabric 主机管理程序开发
# 类 Fabric 主机管理程序开发: # 1. 运行程序列出主机组或者主机列表 # 2. 选择指定主机或主机组 # 3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载) # 4. 充分 ...
- python 学习分享-实战篇简单的ftp
import socket import os import time import pickle Basedb = os.path.dirname(os.path.dirname(os.path.a ...
- Python学习笔记-列表的增删改查
- EF学习笔记-1 EF增删改查
首次接触Entity FrameWork,就感觉非常棒.它节省了我们以前写SQL语句的过程,同时也让我们更加的理解面向对象的编程思想.最近学习了EF的增删改查的过程,下面给大家分享使用EF对增删改查时 ...
随机推荐
- 新客户上云 - 来自 Azure 技术支持部门的忠告
本课程内容是来自 Azure 中国技术支持团队对新客户上云的忠告. 对于上云的新用户,Azure 技术支持部门有如下忠告: 1. 时刻关注并理解以下网站的变动来优化资源配置,更新设计方案. Azure ...
- Hadoop 分片、分组与排序
首先需要明确的是,hadoop里的key一定要是可排序的,要么key自身实现了WritableComparator接口,要么有一个排序类可以对key进行排序.如果key本身不实现WritableCom ...
- yum安装软件并保留下载的软件
使用yum插件downloadonly下载安装软件需要的依赖包并保留到指定的文件 安装yum-downloadonly或 yum-plugin-downloadonly 软件包. yum instal ...
- pta 编程题10 Root of AVL Tree
其它pta数据结构编程题请参见:pta 这道题考察平衡二叉查找树的插入. 为了保证二叉查找树的平衡,当一个结点的左右子树的高度差大于1时就要进行调整. 分为以下四种情况: 插入新节点后,以及旋转之后, ...
- pta 编程题8 Tree Traversals Again
其它pta数据结构编程题请参见:pta 这次的作业考察的是树的遍历. 题目的输入通过栈的pop给出了树的中序遍历的顺序.根据push和pop的顺序构造树的方法为:定义一个变量father来确定父节点, ...
- pat乙级1034
1.vs2013不能用scanf,改为scanf_s,但是提交时不能用scanf_s,用scanf... scanf_s(], &a[], &b[], &b[]); 2.c++ ...
- 【BZOJ1087】[SCOI2005] 互不侵犯King(状压DP)
点此看题面 大致题意: 在\(N×N\)的棋盘里面放\(K\)个国王,使他们互不攻击,共有多少种摆放方案(国王能攻击到它周围的8个格子). 状压\(DP\) 一看到这道题我就想到了经典的八皇后问题,但 ...
- 通过cmd查看环境变量名对应的环境变量值
在VS环境中通常要添加路径,不过基本都是按照往上提供的方法添加变量名形如:$(VC_IncludePath),但是如何通过cmd命令找到真正的路径呢 未完待续……
- Bootstrap历练实例:响应式布局
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Sql优化器究竟帮你做了哪些工作?
关系型数据库的一大优势之一,用户无需关心数据的访问方式,因为这些优化器都帮我们处理好了,但sql查询优化的时候,我不得不要对此进行关注,因为这牵扯到查询性能问题. 有经验的程序员都会对一些sql优化了 ...