【Python之旅】第二篇(五):基于列表、字典和元组的员工信息处理接口

简介:

1.基本需求

    编写一个查询员工信息表的程序,实现如下功能:

(1)让用户输入不小于3个字符查询员工信息

(2)通过员工号或员工个人信息可以精确或模糊查询到员工信息

(3)输出员工信息




2.实现代码与注释

   首先提供员工信息的txt文件:

1
2
3
4
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ more student_info.txt 
stu1101 mingjia.xu  275896019 @qq.com  263  SystemAdmin  18810404260
stu1102 Yangjiansong jason@s156.com A8music SystemAdmin  13601247960
stu1103 zouxinkai zouxinkai_2006@ 126 .com jishubu systemadmin  1861214111

    基于上述需求,利用列表、字典和元组的相关处理函数,编写如下程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python
staff_dic = {}    #从文件中读取员工信息,并作为字典处理
f = file( 'student_info.txt' )
for  line  in  f.xreadlines():
     stu_id, stu_name, mail, company, title, phone = line.split() #取文件一行中每一列元素
     staff_dic[stu_id] = [stu_name, mail, company, title, phone]   #key值对应的Value值为一列表
     
while  True:
     query = raw_input( '\033[32;1mPlease input the query string:\033[0m' ).strip() 
     if  len(query) <  3 :    #如果输入查询的字符少于 3 ,则要求重新输入
         print  'You have to input at least 3 letters to query!'
         continue
     
     match_counter =  0  #计数器,判断是否有匹配到员工信息
     for  k,v  in  staff_dic.items():    #.items(),key值作为列表中,元组的左元素,key值(这里为列表)则作为右元素
         index = k.find(query) #find()返回查询到字符串的首个字符的索引,找空串返回 0 ,找不到返回- 1
         if  k.find(query) != - 1 :  #如果找到
             print k[:index] +  '\033[32;1m%s\033[0m'  % query + k[index + len(query):],v    #这里会有用户输入的内容进行颜色加深
             match_counter +=  1
         else :
             str_v =  '\t' .join(v)  #将列表中的元素连接为字符串
             index = str_v.find(query) #方法如上面查找key值一样
             if  index != - 1 :
                 print k,str_v[:index] +  '\033[32;1m%s\033[0m'  % query + str_v[index + len(query):]
                 match_counter +=  1
 
     print  'Matched \033[31;1m%s\033[0m records!'  % (match_counter)




3.测试

    基于上述的情况,对可能出现的情况和结果,测试如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Please input the query string:stu1101    ===>对员工号(键值)精确查询
stu1101 [ 'mingjia.xu' '275896019@qq.com' '263' 'SystemAdmin' '18810404260' ]
Matched  1  records!
 
Please input the query string:stu    ===>对员工号(键值)模糊查询
stu1103 [ 'zouxinkai' 'zouxinkai_2006@126.com' 'jishubu' 'systemadmin' '1861214111' ]
stu1102 [ 'Yangjiansong' 'jason@s156.com' 'A8music' 'SystemAdmin' '13601247960' ]
stu1101 [ 'mingjia.xu' '275896019@qq.com' '263' 'SystemAdmin' '18810404260' ]
Matched  3  records!
 
Please input the query string:kai    ===>对员工信息(value值)模糊查询
stu1103 zouxinkai  zouxinkai_2006@ 126 .com  jishubu systemadmin  1861214111
Matched  1  records!
Please input the query string:zou
stu1103 zouxinkai  zouxinkai_2006@ 126 .com  jishubu systemadmin  1861214111
Matched  1  records!



本文转自 xpleaf 51CTO博客,原文链接:http://blog.51cto.com/xpleaf/1695146,如需转载请自行联系原作者

相关文章
|
2天前
|
开发者 Python
【Python 基础】递推式构造字典(dictionary comprehension)
【5月更文挑战第8天】【Python 基础】递推式构造字典(dictionary comprehension)
|
3天前
|
BI Python
深入浅出:讲解Python中的列表推导式
深入浅出:讲解Python中的列表推导式
|
3天前
|
监控 PHP Python
1688快速获取整店铺列表 采集接口php Python
在电子商务的浪潮中,1688平台作为中国领先的批发交易平台,为广大商家提供了一个展示和销售商品的广阔舞台;然而,要在众多店铺中脱颖而出,快速获取商品列表并进行有效营销是关键。
|
4天前
|
算法 Python
Python中不使用sort对列表排序的技术
Python中不使用sort对列表排序的技术
16 1
|
4天前
|
Python
【Python 基础】列表(list)和元组(tuple)有什么区别?
【5月更文挑战第6天】【Python 基础】列表(list)和元组(tuple)有什么区别?
|
4天前
|
算法 Python
从原始边列表到邻接矩阵:使用Python构建图的表示
从原始边列表到邻接矩阵:使用Python构建图的表示
8 0
|
28天前
|
开发者 索引 Python
实践:如何使用python在网页的表格里抓取信息
实践:如何使用python在网页的表格里抓取信息
|
定位技术 开发者 Python
Python编程:通过百度地图接口抓取机构的地址和电话信息
Python编程:通过百度地图接口抓取机构的地址和电话信息
489 0
|
数据采集 供应链 机器人
Python - 抓取 iphone13 pro 线下店供货信息并发送到钉钉机器人,最后设置为定时任务
Python - 抓取 iphone13 pro 线下店供货信息并发送到钉钉机器人,最后设置为定时任务
393 0
Python - 抓取 iphone13 pro 线下店供货信息并发送到钉钉机器人,最后设置为定时任务
|
SQL 关系型数据库 数据库连接
使用python3抓取pinpoint应用信息入库
使用python3通过pinpoint api来获取pinpoint中应用基础信息、上下游链路,并入库
1878 0