"Python Tour" chapter II (v): Employee information Processing interfaces based on lists, dictionaries, and tuples

Source: Internet
Author: User

1. Basic Requirements

Write a program to query the Employee Information table to achieve the following functions:

(1) Let the user enter no less than 3 characters to query employee information

(2) Through employee number or employee personal information can be accurate or fuzzy query to employee information

(3) Output employee information



2. Implementing Code and annotations

first, provide the TXT file for employee information:

[Email protected]:/mnt/hgfs/python/day3$ more student_info.txt stu1101 mingjia.xu [email protected] 263 Systemadmin 18810404260stu1102 Yangjiansong [email protected] a8music systemadmin 13601247960stu1103 Zouxinkai [email protected] Jishubu Systemadmin 1861214111

Based on the above requirements, the following programs are written using the related processing functions of lists, dictionaries, and tuples:

#!/usr/bin/env pythonstaff_dic = {} #从文件中读取员工信息 and as a dictionary processing 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 , a re-entry of print  ' you have to input at least 3 letters to query is required !‘ continuematch_counter = 0# counter, determine if there is a match to employee information For k,v in staff_dic.items (): #.items (), The key value as the left element of the tuple in the list, the key value (here as a list) is the right element index = k.find (query) #find () returns the index of the first character of the query to the string, the empty string returns 0, and the returned -1if is not found  k.find (query)  != -1: #如果找到print  k[:index] +  ' \033[32;1m%s\033[0m '  %  Query + k[indEx + len (query):],v     #这里会有用户输入的内容进行颜色加深match_counter  += 1else: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 += 1print  ' matched \033[31;1m%s\033[0m  Records! '  %  (Match_counter)



3. Testing

based on the above situation, the test is as follows for the possible situation and results:

please input the query string:stu1101    ===> accurate query of employee number (key value) stu1101  [' Mingjia.xu ',  ' [email protected] ',  ' 263 ',  ' systemadmin ',  ' 18810404260 ']Matched  1 records! please input the query string:stu    ===> fuzzy query on employee number (key value) stu1103 [ ' Zouxinkai ',  ' [email protected] ',  ' Jishubu ',  ' systemadmin ',  ' 1861214111 ']stu1102  [' Yangjiansong ',  ' [email protected] ',  ' a8music ',  ' systemadmin ',  ' 13601247960 '] stu1101 [' Mingjia.xu ',  ' [email protected] ',  ' 263 ',  ' systemadmin ',  ' 18810404260 '] matched 3 records! please input the query string:kai    ===> Fuzzy query on employee information (value values) stu1103  zouxinkai[email protected]jishubusystemadmin1861214111matched 1 records! Please input the query string:zoustu1103 zouxinkai[email protected]jishubusystemadmin1861214111matched 1 records! 





This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1695146

"Python Tour" chapter II (v): Employee information Processing interfaces based on lists, dictionaries, and tuples

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.