#coding:utf-8 __author__ = 'hdfs' def getDictFromFile(file): info={} with open(file,'r') as f: for line in f: stuid,name,qq,www,score,role=[x.strip() for x in line.split(' ')] info[stuid]=[name,qq,www,score,role] return info def printFindList(list,conn=' '): connprintstr=[] for x in list: pos=x.find(query) if pos!=-1: connprintstr.append(x[:pos]+' 33[32;1m%s 33[0m'%query+x[pos+len(query):]) else: connprintstr.append(x) return conn.join(connprintstr) def colorPrint(query,color): if color=='red': return ' 33[31;1m%s 33[0m'%query if color=='green': return ' 33[32;1m%s 33[0m'%query info=getDictFromFile('D://info.txt') while True: query=raw_input(' 33[32;1m请输入关键字: 33[0m').strip() findcount=0 if len(query)<3: print('你需要输入的关键字至少大于3个字符!') continue for k,v in info.items(): index=k.find(query) if index!=-1: findcount+=1 print(k[:index]+colorPrint(query,color='greed')+k[index+len(query):],v) else: if ''.join(v).find(query)!=-1: findcount+=1 print(k+' '+printFindList(info[k])) print('找到'+colorPrint(findcount,color='red')+'处关键字!') ''' ----------------------------- 请输入关键字:system stu1103 lixaonong 43mnfj@126.com www.google.com 461 system stu1102 zhangsanfeng qrjffd@bi.com www.gl.com 24415 system stu1101 yangwubing 8131415@qq.com www.baidu.com 14545166 system stu1105 lixaonong 43mnfj@126.com www.google.system system system stu1104 lixaonong 43mnfj@126.com www.google.com 461 system ----------------------------- '''