一大波函数来袭

作业要求:

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. SqlServer作业指定目标服务器

    用SSMS生成数据库作业的创建脚本的时候,有一步是sp_add_jobserver操作: EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = ...

  2. python 学习之FAQ:文档内容写入报错

    2017.3.29 FAQ 1. 文档内容写入报错 使用with open() as file: 写入文档时,出现'\xa9'特殊字符写入报错,通过print('\xa9')打印输出“©”. > ...

  3. graylog日志收集过程举例

    graylog的日志收集功与logslash类似,也是需要input-filter-output这样一个过程. 下面举三种最常用的日志记录来说明一下.     1,TCP报文日志         设置 ...

  4. C++ list类详解

    转自:http://blog.csdn.net/whz_zb/article/details/6831817 双向循环链表list list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素 ...

  5. BZOJ 4541: [Hnoi2016]矿区 平面图转对偶图+DFS树

    4541: [Hnoi2016]矿区 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 433  Solved: 182[Submit][Status][ ...

  6. raspberrypi&linux

    Raspberrypi&linux 2018-01-23 19:54:01 Let's go!

  7. JavaScript模块化开发的那些事

    模块化开发在编程开发中是一个非常重要的概念,一个优秀的模块化项目的后期维护成本可以大大降低.本文主要介绍了JavaScript模块化开发的那些事,文中通过一个小故事比较直观地阐述了模块化开发的过程. ...

  8. Aizu 0121 Seven Puzzle(变进制数的完美hash)

    一遍预处理跑完所有情况,O(1)回答就好.状态记录我用的康拓和逆康拓. #include<bits/stdc++.h> using namespace std; ]; ]; ]; int ...

  9. Java基础——动态代理

    1.什么是动态代理? 简单的来说,就是本来让我自己做的事,请给别人来做,这个请的人就是代理对象 那么动态代理就是在程序运行过程中产生这个代理对象,而程序运行中产生的对象就是用反射的来生成一个代理. 举 ...

  10. 关于小程序 input 组件内容显示不全(显示的长度不满 input 宽度)问题

    问题:小程序的input组件经常用到,但在使用input组件的时候会出现一种现象:明明设置了input的宽度,但是输入的内容显示的长度范围却怎么都不到一整个input组件的宽度,而且后面没显示的地方无 ...