#!/usr/bin/python3 #luck import sys filename = input(‘请输入您要搜索的路径及文档名称:‘) if len(filename) == 0: sys.exit(‘不能为空!‘) while True: mubiao = input(‘请输入您要搜索的关键词:‘) if mubiao == ‘exit‘: sys.exit(‘欢迎使用搜索系统!‘) if len(mubiao) == 0: continue mubiao2 = input(‘请输入第二个关键词,忽略按Enter:‘) if len(mubiao2) == 0: with open(filename) as f: Num = 0 for line in f: line = line.strip(‘\n‘) index = line.find(mubiao) if index > -1: Num += 1 print (line[:index] + (‘\033[31;1m%s\033[0m‘ % mubiao) + line[ index + len(mubiao):]) print (‘一共找到了\033[31;1m%s\033[0m;‘ % Num) if mubiao == ‘exit‘: sys.exit(‘欢迎使用搜索系统!‘) else: print (‘搜索2个关键词‘) with open(filename) as f: Num = 0 for line in f: line = line.strip(‘\n‘) index = line.find(mubiao) index2 = line.find(mubiao2) if index > -1 and index2 > -1: Num += 1 print (line[:index] + (‘\033[31;1m%s\033[0m‘ % mubiao) + line[index:index2] + (‘\033[31;1m%s\033[0m‘ % mubiao2) + line[ index2 + len(mubiao2):] ) print (‘一共找到了\033[31;1m%s\033[0m;‘ % Num)
本文出自 “停止奋斗=停止生命” 博客,请务必保留此出处http://53cto.blog.51cto.com/9899631/1871464
原文地址:http://53cto.blog.51cto.com/9899631/1871464