今天写作业,明天后天要在外旅游

写作业写了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-012,Jack Wang,28,13451024608,HR,2015-01-073,Rain Wang,21,13451054608,IT,2017-04-014,Mack Qiao,44,15653354208,Sales,2016-02-015,Rachel Chen,23,13351024606,IT,2013-03-166,Eric Liu,19,18531054602,Marketing,2012-12-017,Chao Zhang,21,13235324334,Administration,2011-08-088,Kevin Chen,22,13151054603,Sales,2013-04-019,Shit Wen,20,13351024602,IT,2017-07-0310,Shanshan Du,26,13698424612,Operation,2017-07-02

转载于:https://www.cnblogs.com/Jack1314/p/10352619.html

Day 21 20190205 老男孩python学习第21天 内容整理相关推荐

  1. Day 1 20190116 老男孩python学习第1天 内容整理

    第一天  学习2小时 写了半小时的自我介绍,跟导师聊了一下,他26岁,比我还小一岁,做了一年的金融量化平台的自动化开发,主要做后台. 花了半小时看了一下课程合同和入学须知,了解了原来学习的预定总时长为 ...

  2. Day 14 20190129 老男孩python学习第14天 内容整理

    码代码,6个小时. # 1. 请用代码实现: 利用下划线将列表的每一个元素拼接成字符串, li = ['alex', 'eric', 'rain'] # li = ['alex', 'eric', ' ...

  3. python学了真的很有用吗-学习Python真的有必要参加培训吗?老男孩Python学习机构...

    零基础学习Python真的能学会吗?我想很多人都具有这样的疑问吧.其实Python是非常适合初学者入门学习的编程语言,相比较其他主流的编程语言来说,可读性高,上手容易.而且Python具有非常丰富的库 ...

  4. 11.15-11.21【大学生Python学习】社区总结+优秀社区成员点名表扬技术书籍和CSDN定制背包奖励~

      欢迎每一位小伙伴的加入: 社区:大学生Python学习   文章目录: 11.15-11.21的社区总结: 优秀群成员点名表扬&&技术书籍和CSDN定制背包奖励: 排名第一位:[X ...

  5. 学python就业要看哪些书-学习Python适合就业哪些岗位?老男孩Python学习

    简单的来说,Python是一门面向对象的编程语言,最大的特点就是语法简单.上手容易.功能强大,也因此受到了大家的喜欢.而且它具有丰富和强大的库,常被称为 "胶水语言",能够把用其他 ...

  6. python最适合做什么-学习Python适合就业哪些岗位?老男孩Python学习

    简单的来说,Python是一门面向对象的编程语言,最大的特点就是语法简单.上手容易.功能强大,也因此受到了大家的喜欢.而且它具有丰富和强大的库,常被称为 "胶水语言",能够把用其他 ...

  7. 5年Python整理全面Python学习路线 (附自我整理资料)

    本文主要涵盖了 Python 编程的核心知识(暂不包括标准库及第三方库). 首先,按顺序依次展示了以下内容的一系列思维导图:基础知识,数据类型(数字,字符串,列表,元组,字典,集合),条件&循 ...

  8. Python学习笔记(21)-目录遍历

    点此查看 零基础Python全栈文章目录及源码下载 本文目录 1. 简介 2. 实例 1. 简介 遍历一个目录下所有的文件夹和文件是非常有用的事情,在python中os模块提供了非常简单易行的遍历方法 ...

  9. 老男孩python学习_day002作业

    1. 判断下列逻辑语句的True,False. (1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6   ...

最新文章

  1. 可逼近信道容量编码技术之霍夫曼编码的实现
  2. 理解LSTM/RNN中的Attention机制
  3. 算法工程师特大福利 | 不用买云了!这里GPU计算资源免费送!
  4. Ajax Toolkit Control ——CollapsiblePanelExtender(隐藏显示效果)
  5. Python读写docx文件(三十五)
  6. 指针、数组、函数阶段小结
  7. 中石油训练赛 - 腿部挂件(可持久化字典树)
  8. 14-项目开发总结报告(GB8567——88)
  9. 微信wx.request
  10. cms文章 mysql存储_MySQL存储引擎笔记
  11. 使用Arduino和超声波传感器实现简单测距
  12. mysql学习day01
  13. Hibernate ——二级缓存
  14. apache 配置用户级目录
  15. 【UVa10674】Tangents(两圆公切线的切点--验板子题)
  16. linux程序开发ide,LiteIDE 开发工具指南 (Go语言开发工具)
  17. 从卫星影像的视角见证莆田母亲河(美丽的木兰溪)改造前后的容颜变化
  18. 安防摄像头RTSP/Onvif协议网页无插件直播视频流媒体服务器EasyNVR之按需直播如何有效利用最大上行带宽
  19. cdn转发防攻击_cdn可以防止攻击吗
  20. 探索 TDengine在《图码联侦》项目中的应用可行性及实践研究(new)

热门文章

  1. 反转数字(qduoj)
  2. python多进程与多线程实验
  3. spring项目搭建云服务器,Spring Boot项目打包并部署到云服务器
  4. 三线城市PHP5000怎么样,一线城市五千退休金,二线四千,三线三千,是一样生活水平吗?...
  5. 重写toString()方法(Java篇)
  6. Java学习笔记_继承
  7. linux usb xhci ehci,ehci和xhci有什么区别
  8. 线程安全存储以及pthread_getspecific/pthread_setspecific
  9. linux下的各种shell介绍(bash和dash转换)
  10. On the Difference Between Orthogonal Matching Pursuit and Orthogonal Least Squares