Python開發【第xxx篇】函數練習題-----員工信息表
阿新 • • 發佈:2017-09-04
選擇執行 lex utf 返回 false 數據 int 位置 files
文件存儲格式如下:
id,name,age,phone,job
1,Alex,22,13651054608,IT
2,Egon,23,13304320533,Tearcher
3,nezha,25,1333235322,IT
現在需要對這個員工信息文件進行增刪改查。
基礎必做:
a.可以進行查詢,支持三種語法:
select 列名1,列名2,… where 列名條件
支持:大於小於等於,還要支持模糊查找。
示例:
select name,age where age>22 #> <
select * where job=IT # =
select * where phone like 133 #看起來像 ‘abc’in ‘1232abcahj’
#充分的利用函數
#文件處理 字符串處理
#str #where split
進階選做:
b.可創建新員工記錄,id要順序增加
c.可刪除指定員工記錄,直接輸入員工id即可
d.修改員工信息
語法:set 列名=“新的值” where 條件
#先用where查找對應人的信息,再使用set來修改列名對應的值為“新的值”
註意:要想操作員工信息表,必須先登錄,登陸認證需要用裝飾器完成
其他需求盡量用函數實現
1 sos=True 2 def krapper(flot): 3 def inner(*args,**kwargs): 4 globalView Codesos 5 while sos: 6 x=input(‘說賬號:‘) 7 y=input(‘說密碼:‘) 8 with open(‘user‘,encoding=‘utf-8‘) as files: 9 for line in files: 10 val=line.strip().split(‘|‘) 11 if val[0]==x and int(val[1])==int(y):12 print(‘登錄成功‘) 13 sos=False 14 break 15 else: 16 print(‘錯啦,再來‘) 17 ret=flot(*args,**kwargs) 18 return ret 19 return inner 20 # 此程序為初始的登錄功能項,能鏈接到各個分功能 21 @krapper 22 def MySQL(): 23 print(‘請選擇執行的《功能項》||查詢||、||修改||、||創建||、||刪除||‘) 24 choice = input(‘>>>‘) 25 if choice == ‘查詢‘: 26 select() 27 elif choice == ‘修改‘: 28 revise() 29 elif choice == ‘創建‘: 30 appd() 31 elif choice == ‘刪除‘: 32 cut() 33 # 此程序為具有查詢功能的函數 34 @krapper 35 def select(): 36 print("""‘select name,age where age>22 例如:> < ‘ 37 ‘select * where job=IT 38 ‘select * where phone like 133 例如:‘abc’in ‘1232abcahj’‘ 39 ‘退出操作請直接輸入||q||,返回《功能項》請直接輸||回程|| 40 """) 41 stop = True 42 while stop: 43 search = input(‘請按照上述格式所述方法輸入內容:‘) 44 if search==‘q‘: 45 stop=False 46 elif search==‘回程‘: 47 MySQL() 48 else: 49 dic={‘id‘:0,‘name‘:1,‘age‘:2,‘phone‘:3,‘job‘:4} 50 show_list=[] 51 handle=search.strip().split(‘where‘) #[‘...name,age‘,‘age>22‘] 52 with open(‘員工信息表‘,‘r‘,encoding=‘utf-8‘) as files_1: 53 for line in files_1: 54 line_x=line.strip().split(‘,‘) #[1,name,age,phone,job] 55 if ‘>‘ in handle[1]: 56 handle_1=handle[1].strip().split(‘>‘) #[‘age‘,‘22] 57 handle_2=handle[0].replace(‘select‘,‘‘).strip().split(‘,‘) #[‘name‘,‘age‘] 58 if int(line_x[dic[handle_1[0]]]) > int(handle_1[1]) : 59 show_list.append((line_x[dic[handle_2[0]]],line_x[dic[handle_2[1]]]),) 60 elif ‘<‘ in handle[1]: 61 handle_1=handle[1].strip().split(‘<‘) #[‘age‘,‘25] 62 handle_2=handle[0].replace(‘select‘,‘‘).strip().split(‘,‘) #[‘name‘,‘age‘] 63 if int(line_x[dic[handle_1[0]]]) < int(handle_1[1]) : 64 show_list.append((line_x[dic[handle_2[0]]],line_x[dic[handle_2[1]]]),) 65 elif ‘=‘ in handle[1]: 66 handle_1=handle[1].strip().split(‘=‘) #[‘job‘,‘IT‘] 67 if line_x[dic[handle_1[0]]] == handle_1[1] : 68 show_list.append(line_x) 69 elif ‘like‘in handle[1]: 70 handle_1 = handle[1].strip().split(‘like‘) # [‘phone‘,‘133‘] 71 if handle_1[1].strip() in line_x[dic[handle_1[0].strip()]]: 72 show_list.append(line_x) 73 else: 74 print(‘查詢成功,龍龍好帥‘) 75 print(show_list) 76 return 77 # 此程序為具有修改功能的函數 78 @krapper 79 def revise(): 80 dic = {‘id‘: 0, ‘name‘: 1, ‘age‘: 2, ‘phone‘: 3, ‘job‘: 4} 81 print(""">>>請選擇示例中的一個元素來識別要修改員工的信息: 82 >>>示例:id=xx,name=xx,age=xx,phone=xx,job=xx;xx為要輸入內容 83 >>>輸入||q||,退出操作;輸入||回程||返回《功能項》 84 >>>正確輸入格式為:name=xx,想替換的內容phone=yy,替換後的內容zz 85 >>>既:name=xx,phone=yy,zz 86 """) 87 stop = True 88 while stop: 89 print(‘輸入q,退出操作‘) 90 x = input(‘輸入想替換的內容:‘) 91 if x == ‘q‘: 92 stop = False 93 elif x==‘回程‘: 94 MySQL() 95 else: 96 lis_cut = x.strip().split(‘,‘) # [name=xx,phone=yy,zz] 97 lis_cut_1 = lis_cut[0].split(‘=‘) # [name,xx] 98 lis_cut_2 = lis_cut[1].split(‘=‘) # [phone,yy] 99 with open(‘員工信息表‘, ‘r‘, encoding=‘utf-8‘) as files_r: 100 with open(‘員工信息表_bak‘, ‘w‘, encoding=‘utf-8‘) as files_w: 101 for line in files_r: 102 line_lis = line.strip().split(‘,‘) 103 if line_lis[1] == lis_cut_1[1]: 104 line_r = line.replace(str(line_lis[dic[lis_cut_2[0]]]), str(lis_cut[2])) 105 files_w.write(line_r) 106 else: 107 files_w.write(line) 108 import os 109 os.remove(‘員工信息表‘) 110 os.rename(‘員工信息表_bak‘, ‘員工信息表‘) 111 print(‘修改成功,龍龍巨帥‘) 112 return 113 # 此程序為具有添加功能的函數 114 @krapper 115 def appd(): 116 stop = True 117 while stop: 118 print(""">>>按照此順序和方法輸入增加信息 119 >>>示例:name,age,phone,job 120 >>>示例:xxxx,yyy,zzzzz,ooo 121 """) 122 inner = input(‘>>>‘) 123 if inner == ‘q‘ : 124 stop = False 125 elif inner==‘回程‘: 126 MySQL() 127 else: 128 with open(‘員工信息表‘, ‘r‘, encoding=‘utf-8‘) as files_r: 129 with open(‘員工信息表‘, ‘a‘, encoding=‘utf-8‘) as files_a: 130 start=0 131 for line in files_r: 132 start+=1 133 else: 134 line_add=‘\n‘+ str(start+1)+str(‘,‘)+str(inner) 135 files_a.write(line_add) 136 print(‘添加成功,龍龍超級帥‘) 137 return 138 # 此程序為具有刪除功能的函數,可實現序號更新 139 @krapper 140 def cut(): 141 print(""">>>請選擇示例中的兩個元素來識別要刪除的員工信息: 142 >>>示例:name=xx,age=xx,phone=xx,job=xx 143 >>>其中,xx為要輸入內容 144 """) 145 dic = {‘name‘: 0, ‘age‘: 1, ‘phone‘: 2, ‘job‘: 3} 146 stop = True 147 while stop: 148 cutting = input(‘>>>請輸入您想刪除的內容,輸入q則退出操作‘) 149 if cutting == ‘q‘: 150 stop = False 151 elif cutting==‘回程‘: 152 MySQL() 153 else: 154 lis_cut = cutting.strip().split(‘,‘) # [name=xx,age=xx] 155 lis_cut_1 = lis_cut[0].split(‘=‘) # [name,xx] 156 lis_cut_2 = lis_cut[1].split(‘=‘) # [age,xx] 157 with open(‘員工信息表‘, ‘r‘, encoding=‘utf-8‘) as files_r: 158 with open(‘員工信息表_bak‘, ‘w‘, encoding=‘utf-8‘) as files_w: 159 start = 0 160 for line in files_r: 161 line_r = line.strip().split(‘,‘) 162 line_r.remove(line_r[0]) 163 if line_r[dic[lis_cut_1[0]]] == lis_cut_1[1] and line_r[dic[lis_cut_2[0]]] == lis_cut_2[1]: 164 continue 165 else: 166 start += 1 167 line_all = str(start) + ‘,‘ + str(line_r[0]) + ‘,‘ + str(line_r[1]) + ‘,‘ + str( 168 line_r[2]) + ‘,‘ + str(line_r[3]) + ‘\n‘ 169 files_w.write(line_all) 170 import os 171 os.remove(‘員工信息表‘) 172 os.rename(‘員工信息表_bak‘, ‘員工信息表‘) 173 print(‘刪除成功,龍龍無敵帥‘) 174 return 175 # 此處的mysql()後期可指向固定位置,對文件做各種操作,具有簡單數據庫功能 176 MySQL()
Python開發【第xxx篇】函數練習題-----員工信息表