员工信息表
- 员工可以模糊查询员工信息
- 显示匹配了多少条,匹配字符需要高亮显示
$ vim contact.py
contact_dic = {}
with open('contact_list2.txt') as f:
for i in f.readlines(): # loop file content
line = i.strip().split() #qu hangshou he hangwei de kongge
contact_dic[line[0]] = line[1:] #lieBiao(list) zhuanWei ziDian(dict)
print contact_dic.keys()
while True:
search = raw_input("Search info:").strip()
if len(search) == 0: continue #not empty input
# print len(search)
if contact_dic.has_key(search):
print search, contact_dic[search]
else: # start to search the info in fuzzy matching mode
info_counter = 0
if len(search) < 3:
print "NO valid info..."
continue
for name,value in contact_dic.items():
if name.count(search) != 0: #exit
s_index = name.find(search)
print name[:s_index] + "\033[32;1m%s\033[0m" %search + name[s_index+len(search):],'\t'.join(value)
#print name,'\t'.join(value) #list zhuan char
info_counter +=1
for i in value:
if i.count(search) != 0:
print name,'\t'.join(value)
info_counter +=1
if info_counter ==0:
print "no valid record..."
else:
print "Fount %s records..." % info_counter