#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=‘\t‘): connprintstr=[] for x in list: pos=x.find(query) if pos!=-1: connprintstr.append(x[:pos]+‘\033[32;1m%s\033[0m‘%query+x[pos+len(query):]) else: connprintstr.append(x) return conn.join(connprintstr) def colorPrint(query,color): if color==‘red‘: return ‘\033[31;1m%s\033[0m‘%query if color==‘green‘: return ‘\033[32;1m%s\033[0m‘%query info=getDictFromFile(‘D://info.txt‘) while True: query=raw_input(‘\033[32;1m请输入关键字:\033[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+‘\t‘+printFindList(info[k])) print(‘找到‘+colorPrint(findcount,color=‘red‘)+‘处关键字!‘) ‘‘‘ ----------------------------- 请输入关键字:system stu1103 lixaonong [email protected] www.google.com 461 system stu1102 zhangsanfeng [email protected] www.gl.com 24415 system stu1101 yangwubing [email protected] www.baidu.com 14545166 system stu1105 lixaonong [email protected] www.google.system system system stu1104 lixaonong [email protected] www.google.com 461 system ----------------------------- ‘‘‘
时间: 2024-11-02 19:37:52