一大波函数来袭

作业要求:

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 学习分享-实战篇增删改查作业的更多相关文章

  1. python学习之-成员信息增删改查

    python学习之-成员信息增删改查 主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env p ...

  2. pymongo学习第1篇——增删改查

    参考文档: 1.https://docs.mongodb.org/getting-started/python/ 2.http://api.mongodb.org/python/current/api ...

  3. python学习之成员信息增删改查

    主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env python# coding=utf8# ...

  4. python 学习分享-实战篇高级的ftp

    #server代码 import socketserver,os,hashlib Base_paht = os.path.dirname(os.path.dirname(os.path.abspath ...

  5. python 学习分享-实战篇选课系统

    # 角色:学校.学员.课程.讲师 # 要求: # 1. 创建北京.上海 2 所学校 # 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 # ...

  6. python 学习分享-实战篇类 Fabric 主机管理程序开发

    # 类 Fabric 主机管理程序开发: # 1. 运行程序列出主机组或者主机列表 # 2. 选择指定主机或主机组 # 3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载) # 4. 充分 ...

  7. python 学习分享-实战篇简单的ftp

    import socket import os import time import pickle Basedb = os.path.dirname(os.path.dirname(os.path.a ...

  8. Python学习笔记-列表的增删改查

  9. EF学习笔记-1 EF增删改查

    首次接触Entity FrameWork,就感觉非常棒.它节省了我们以前写SQL语句的过程,同时也让我们更加的理解面向对象的编程思想.最近学习了EF的增删改查的过程,下面给大家分享使用EF对增删改查时 ...

随机推荐

  1. Visual Studio 2017 如何打开Model Browser(实体数据模型浏览器)

    写一个笔记,记录下在Visual Studio 2017中打开EF模型浏览器的步骤和方法,方便以后忘记了可以重新查阅.主要是现在VS功能越来越多,很多功能模块/界面要开启都是有先决条件,总之隐藏的很深 ...

  2. MVC下c#对接微信公众平台开发者模式

    在ashx文件中进行HttpContext的处理: using System; using System.Collections.Generic; using System.Linq; using S ...

  3. pycharm tab换为4个空格

    Edit => find => replace 然后勾上 Regex,上一行输入 \t,下一行输入4个空格.

  4. 【洛谷1337】[JSOI2004] 吊打XXX(模拟退火经典题)

    点此看题面 大致题意: 一个平面上有\(n\)个点,每个点有1个权值,现在要选择平面上的一个点,使这\(n\)个点的权值乘上到达选定点的距离之和最小. 模拟退火 我们可以用模拟退火来做这道题. 先将\ ...

  5. 从用户访问网站流程开始,细说web网络基础

    1.用户访问网站流程框架 2.dns解析原理 3.tcp/ip三次握手过程原理,11种连接状态 4.tcp/ip四次挥手过程原理,11种连接状态 5.http协议原理(www服务的请求过程)请求细节, ...

  6. python_52_函数返回值2

    def test1(x,y): print(x,y) test1(1,2)#位置参数调用,按顺序来,与形参一一对应 test1(y=1,x=2)#输出为2 1,不是1 2.关键字参数调用按关键字,不按 ...

  7. JSP出现"属性值[request.getParameter("myMessage")]引用["],在值内使用时必须被转义"的解决方法

    写JSP时出现属性值[request.getParameter("myMessage")]引用["],在值内使用时必须被转义. 源代码: <jsp:setPrope ...

  8. 题解 P3367 【【模板】并查集】

    #include<iostream> #include<cstdio> using namespace std; int n,m,x,y,z; ]; //f[i]表示i的祖先 ...

  9. JSTree下的模糊查询算法——树结构数据层次遍历和递归分治地深入应用

    A表示区域节点,S表示站点结点 问题描述:现有jstree包含左图中的所有结点信息(包含区域结点和站点结点),需要做到输入站点名称模糊查询,显示查询子树结果如右图 解决策略: 1.先模糊查询所得站点所 ...

  10. 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)

    微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...