日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Day 21 20190205 老男孩python学习第21天 内容整理

發布時間:2023/12/13 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Day 21 20190205 老男孩python学习第21天 内容整理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天寫作業,明天后天要在外旅游

?

寫作業寫了7個小時。

?

1 def read_file_as_dict(where): 2 staff_dict = {} 3 f = open('%s' % where, mode="r+", encoding='utf-8') 4 data = f.read() 5 f.close() 6 row = data.strip().split('\n') 7 for staff in row: 8 staff_info = staff.split(',') # 每一條是這樣的: ['1', 'Alex Li', '22', '13651054608', 'IT', '2013-04-01'] 9 staff_dict[staff_info[3]] = staff_info 10 return staff_dict 11 12 13 def save_back_to_file(where): 14 s = staff_dict.values() 15 s2 = '' 16 for staff in s: 17 for info in staff: 18 s2 = s2 + info + ',' 19 s2 = s2.rstrip(',') + '\n' 20 s2.rstrip('\n') 21 22 f = open('%s' % where, mode="w", encoding='utf-8') 23 f.writelines(s2) 24 f.close() 25 26 27 def find_staff(how): 28 # 首先先篩選,輸出需要打印的員工名單,即在staff_list中的index號 29 # how[0]為目標比較項,如age,name等,how[1]為比較符,如大于小于等于等等,how[2]為去比較的值,如年齡22,電話號碼等 30 selected_staff = [] 31 if how[0] == 'staff_id' or how[0] == 'age': # 如果比較的項是員工ID或者年齡這類數字 32 if how[1] == '>': 33 for key, value in staff_dict.items(): 34 if int(value[title_list.index(how[0])]) > int(how[2]): # 篩選完畢 35 selected_staff.append(key) # 輸出符合條件的所有staff的index的列表selected_staff_index 36 return selected_staff 37 38 elif how[1] == '<': 39 for key, value in staff_dict.items(): 40 if int(value[title_list.index(how[0])]) < int(how[2]): # 篩選完畢 41 selected_staff.append(key) 42 return selected_staff 43 44 elif how[1] == '=': 45 for key, value in staff_dict.items(): 46 if int(value[title_list.index(how[0])]) == int(how[2]): # 篩選完畢 47 selected_staff.append(key) 48 return selected_staff 49 50 elif how[0] == 'enroll_date' and how[1] == 'like': # 比較入職日期,日期目前只有like+年份的操作 51 for key, value in staff_dict.items(): 52 if value[title_list.index(how[0])].startswith(how[2]): # 篩選完畢 53 selected_staff.append(key) 54 return selected_staff 55 56 elif how[0] == 'name' and how[1] == '=': # 比較姓名 57 target_name = ' '.join(how[2:]) # 把已經拆分成兩個列表元素的姓與名合并起來 58 for key, value in staff_dict.items(): 59 if value[title_list.index(how[0])] == target_name: # 篩選完畢 60 selected_staff.append(key) 61 return selected_staff 62 63 elif how[0] in title_list and how[1] == '=': # 如果條件是除上述以外的項目,如比較電話、部門 64 for key, value in staff_dict.items(): 65 if value[title_list.index(how[0])] == how[2]: # 篩選完畢 66 selected_staff.append(key) 67 return selected_staff 68 69 else: 70 print('對不起,您的輸入有誤,請重新輸入!') 71 72 73 def print_info(what, list): # 如print_info('name,age', [1, 3, 8])或者print_info('*', [1, 3, 8]) 74 75 # 首先是識別what參數傳入進來需要顯示的項目名稱, 76 index_list = [] 77 s = '' 78 global title_list # 呼叫全局變量,用作后續對比 79 subset_flag = True # 標識符,查看用戶輸入信息是否合法 80 81 if what == '*': # 輸入為*號,即打印所有信息 82 index_list = range(len(title_list)) 83 84 else: 85 what = what.split(',') # 如果傳進去what是'age, name'的話,這里就變成了['age', 'name'] 86 for element in what: # 先看一下用戶傳入的需打印的title是不是都是可打印的title,與title_list進行匹配 87 if element in title_list: 88 index_list.append(title_list.index(element)) # 將需打印的title在title_list當中的索引做成一個新列表,供打印使用 89 else: 90 subset_flag = False 91 92 if subset_flag: # 如果沒有問題,都是合法的title 93 for i in selected_staff: # 將每個要打印的員工循環 94 for index in index_list: # 將每個員工的每個要打印的項目循環 95 s = s + title_list[index] + ' ' + staff_dict[i][index] + ' ' # 將同一個員工的信息加在同一行 96 97 s = s + '\n' # 每個員工循環完畢,換行 98 99 print(s) # 打印全部需要打印的信息 100 else: 101 print('對不起,您的輸入有誤,請重新輸入!') 102 103 104 def add_staff(words, staff_dict): 105 new_staff_id = str(len(staff_dict) + 1) # 新的staff_id就是現有的數量+1,為了后面寫數據方便,格式轉成字符串 106 r_detailed_info = words[3].split(',') # 將除了姓以外的所有數據處理,結果如[Li', '25', '134435344', 'IT', '2015-10-29'] 107 r_detailed_info[0] = words[2] + ' ' + r_detailed_info[0] # 將姓氏加上 108 r_detailed_info.insert(0, new_staff_id) # 插入新的staff_id 109 if r_detailed_info[3] in staff_dict: 110 print('對不起,您想要添加的手機號已經存在,無法重復添加!') 111 else: 112 staff_dict[r_detailed_info[3]] = r_detailed_info # 將新的員工信息,手機號作為鍵,所有信息的列表作為值,插入字典 113 114 save_back_to_file(words[1]) 115 print('新員工信息%s,共1條已加入系統' % r_detailed_info) 116 117 118 def del_staff(selected_staff, staff_dict): 119 for staff in selected_staff: # 將查找到的員工信息刪除 120 deleted_staff = staff_dict.pop(staff) 121 122 id_bigger_than_del_list = find_staff(['staff_id', '>', words[6]]) # 刪除后只影響它后面的staff_id,需要-1 123 for key in id_bigger_than_del_list: # 更新staff_id 124 staff_dict[key][0] = str(int(staff_dict[key][0]) - 1) 125 126 save_back_to_file(words[2]) 127 print('老員工信息%s,共1條已刪除' % deleted_staff) 128 129 130 def update_staff(selected_staff, staff_dict): 131 global title_list 132 for staff in selected_staff: # 將查找到的員工信息更新 133 staff_dict[staff][title_list.index(words[3])] = words[5] 134 save_back_to_file(words[1]) 135 print('所有%s為%s的員工,%s已經變更為%s,共變更%s條' % (words[7], words[9], words[3], words[5], len(selected_staff))) 136 137 138 # --------------------------------------------函數區結束,下面為實現代碼區--------------------------------------------- 139 title_list = ['staff_id', 'name', 'age', 'phone', 'dept', 'enroll_date'] 140 141 while True: 142 search_instruction = input('please type in your searching instruction:').strip().replace('"', '') 143 words = search_instruction.split( 144 ' ') # words = ['find', 'name,age', 'from', 'staff_table', 'where', 'age', '>', '22'] 145 146 if words[0] == 'find' and 8 <= len(words) <= 9: # 命令的第一個字符串識別是哪一類操作,如果為find,開始進行find功能,將關鍵參數傳入函數,函數命令元素一般為8個,查姓名為9個 147 148 staff_dict = read_file_as_dict(words[3]) # 調用read_file函數讀取文檔,生成對應的列表文件 149 # 列表類似staff_list = [['1', 'Alex Li', '22', '13651054608', 'IT', '2013-04-01'], 150 # ['2', 'Jack Wang', '28', '13451024608', 'HR', '2015-01-07']] 151 152 selected_staff = find_staff(words[5:]) # words[5:]為條件str語句的列表['age', '>', '22'] 153 # 輸出的這個selected_staff的列表內容為[1, 2, 3, 5]等等的被篩選后需要打印信息的員工在staff_list里面的索引 154 155 # print(selected_staff) 156 157 print_info(words[1], selected_staff) # 然后是利用傳入進來的list,打印list里每個人的what項目的信息,每人一行 158 159 print('這條語句查詢了%s條' % len(selected_staff)) 160 161 elif words[0] == 'add' and len( 162 words) == 4: # 姓占1個長度,['add', 'staff_table', 'Alex', 'Li,25,134435344,IT,2015-10-29'] 163 staff_dict = read_file_as_dict(words[1]) # 將change_file函數的返回值字典賦予staff_dict 164 add_staff(words, staff_dict) 165 166 elif words[0] == 'del' and len(words) == 7: # ['del', 'from', 'staff_table', 'where', 'id', '=', '3'],命令長度為7 167 168 staff_dict = read_file_as_dict(words[2]) 169 words[4] = 'staff_id' # 將id改為staff_id以配合功能查詢 170 selected_staff = find_staff(words[4:]) # 用find_staff功能進行查詢 171 if not selected_staff: # 如果selected_staff值為空, 172 print('對不起,系統沒有查詢到,請重新查詢!') 173 else: # 如果selected_staff值不為空, 174 del_staff(selected_staff, staff_dict) 175 176 elif words[0] == 'update' and len( 177 words) == 10: # 如['update', 'staff_table', 'set', 'dept', '=', 'Market', 'where', 'dept', '=', 'IT'] 178 staff_dict = read_file_as_dict(words[1]) 179 selected_staff = find_staff(words[7:]) 180 if not selected_staff: # 如果selected_staff值為空, 181 print('對不起,系統沒有查詢到,請重新查詢!') 182 else: # 如果selected_staff值不為空, 183 update_staff(selected_staff, staff_dict) 184 185 elif search_instruction == 'q': 186 exit('感謝您的使用,再見!') 187 188 else: 189 print('對不起,系統無法識別您的命令,請重新輸入!') View Code staff_table:

1,Alex Li,22,13651054608,IT,2013-04-01
2,Jack Wang,28,13451024608,HR,2015-01-07
3,Rain Wang,21,13451054608,IT,2017-04-01
4,Mack Qiao,44,15653354208,Sales,2016-02-01
5,Rachel Chen,23,13351024606,IT,2013-03-16
6,Eric Liu,19,18531054602,Marketing,2012-12-01
7,Chao Zhang,21,13235324334,Administration,2011-08-08
8,Kevin Chen,22,13151054603,Sales,2013-04-01
9,Shit Wen,20,13351024602,IT,2017-07-03
10,Shanshan Du,26,13698424612,Operation,2017-07-02

轉載于:https://www.cnblogs.com/Jack1314/p/10352619.html

總結

以上是生活随笔為你收集整理的Day 21 20190205 老男孩python学习第21天 内容整理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。