【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!

相关文章
|
4月前
|
索引 Python 存储
Python 04 之变量【列表,元组,集合,字典,字符串】
Python 04 之变量【列表,元组,集合,字典,字符串】
82 0
Python 04 之变量【列表,元组,集合,字典,字符串】
|
1月前
|
存储 索引 Python
Python学习笔记----列表、元组和字典的基础操作
这篇文章是一份Python学习笔记,涵盖了列表、元组和字典的基础操作,包括它们的创建、修改、删除、内置函数和方法等。
Python学习笔记----列表、元组和字典的基础操作
|
23天前
|
存储 Python
Python 中的列表和元组
【8月更文挑战第29天】
19 1
|
24天前
|
Python
Python多维列表(元组)合并成一维形式
Python多维列表(元组)合并成一维形式
14 2
|
1月前
|
存储 索引 Python
五:《Python基础语法汇总》— 列表&元组&集合
本篇文章讲解了关于列表;元组和集合这三个基本数据类型的常用方法与函数。及同一性操作符;成员判断符;浅拷贝与深拷贝等多方面的知识点
24 4
|
2月前
|
存储 缓存 测试技术
Python列表与元组
【7月更文挑战第26天】Python 中的列表(List)和元组(Tuple)是两种常用的数据结构,它们都可以用来存储一系列的元素。虽然它们在某些方面相似,但也有一些重要的区别。在本文中,我们将分享一些 Python 中列表和元组的操作技巧,帮助您更好地理解它们的用法和特性。
30 4
|
2月前
|
存储 缓存 Python
Python中的列表(List)和元组(Tuple)是两种重要的数据结构
【7月更文挑战第12天】Python中的列表(List)和元组(Tuple)是两种重要的数据结构
36 1
|
3月前
|
Python
|
3月前
|
Python
python 中列表和元组的解包
【6月更文挑战第17天】
34 1
|
3月前
|
存储 索引 Python
Python零基础入门-5 数据结构(列表和元组
Python零基础入门-5 数据结构(列表和元组