一大波函数来袭
作业要求:
1本次作业通过空格及逗号,将文件拆分成列表,在通过判断add、del、update、select等关键字,来判断用户执行的是哪种命令,根据不同的命令调用不同的函数去处理。
2增加操作及删除操作较为简单,根据规范来执行即可,查询及修改操作较为复杂。
3查询函数,首先要进行判断=及like是否在其中,根据这一点来确定select方式,然后判断关键字的位置,即其在列表中的位置。通过这些就可以查询出内容。
4修改函数类似于查询函数,因为修改操作比较单一,所以只需要判断关键字即可。
5最后就是主程序函数的编写,根据关键字判断是哪种操作,然后执行即可。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158import 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('33[34;0m【staff_id】【name】【age】【phone】【dept】【enroll_date】33[0m') for i in staff_table: print(i) def input_get(): #input order函数 print('ORDER'.center(60,'=')) add_order = 'add name=33[1;31;0mLaay33[0m age=33[1;31;0m1833[0m phone=33[1;31;0m1356363012933[0m dept=33[1;31;0mIT33[0m' del_order = 'del 33[1;31;0m133[0m' upd_order = 'update staff_table set 33[1;31;0mdept33[0m = Market where dept = 33[1;31;0mIT33[0m' sel_order_1 = 'select 33[1;31;0mname,age33[0m from staff_table where 33[1;31;0mage > 2233[0m' sel_order_2 = 'select 33[1;31;0m*33[0m from staff_table where 33[1;31;0mdept = IT33[0m' sel_order_3 = 'select 33[1;31;0m*33[0m from staff_table where 33[1;31;0menroll_date like 201333[0m' print('增加=> ',add_order) print('删除=> ',del_order) print('修改=> ',upd_order) print('查询=> ',sel_order_1) print('查询=> ',sel_order_2) print('查询=> ',sel_order_3) print('退出=> ','33[1;32;0mq33[0m') print('INPUT ORDER'.center(60, '=')) input_order = input('33[31;0m Input your order:33[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('33[36;3m%s33[3m'%(z)) print('共计有33[35;3m{}33[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('33[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()
转载于:https://www.cnblogs.com/laay/p/6501949.html
最后
以上就是机智百合最近收集整理的关于python 学习分享-实战篇增删改查作业的全部内容,更多相关python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复