查询功能

 1 tag = True
 2 while tag:
 3     # print('请输入:1\n2\n3\n4\n')
 4     if tag:
 5         print('level')
 6         choice = input('请输入:')
 7         if choice=='quit':
 8             break
 9         elif choice=='quit_all':
10             tag = False
11         while tag:
12             print('level2')
13             choice = input('请输入:')
14             if choice == 'quit':
15                 break
16             elif choice == 'quit_all':
17                 tag = False
18             while tag:
19                 print('level3')
20                 choice = input('请输入:')
21                 if choice == 'quit':
22                     break
23                 elif choice == 'quit_all':
24                     tag = False
25 
26 def fetch(data):
27     print('这是查询到的内容')
28     # print('%s这是要查询的数据' %data)
29     backend_data = 'backend %s' %data
30     with open('haproxy.conf','r') as read_f:
31         tag = False
32         for read_line in read_f:
33             if read_line.strip() == backend_data:
34                 tag = True          #如果输入内容和查找到的内容相同改变tag状态并重新循环
35                 continue
36             if tag and read_line.startswith('backend'):     
37                 break               #如果遇到了下一个backend,终止循环
38             if tag:
39                 print(read_line,end='')     #一条一条打印,打印到break出现为止
40 
41 def add():
42     print('这是添加')
43 
44 def change():
45     print('这是更改')
46 
47 def delete():
48     print('这是删除')
49 
50 if __name__ == '__main__':
51     msg='''
52     1:查询
53     2:添加
54     3:修改
55     4:删除
56     5:退出
57     '''
58     msg_dic = {
59         '1':fetch,
60         '2':add,
61         '3':change,
62         '4':delete
63     }
64     while True:
65         print(msg)
66         choice = input('请输入:').strip()
67         if not choice:continue                  #如果用户没有输入内容则重新循环
68         if choice == '5':break
69         data = input('请输入要查询的内容')
70         msg_dic[choice](data)       #假如用户输入的是'1',那么就变成msg_dic[1]() == fetch()函数

猜你喜欢

转载自www.cnblogs.com/humanskin/p/8874277.html