今天写作业,明天后天要在外旅游
写作业写了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