您的位置:首页 > 编程语言 > Python开发

Python查找字符串高亮显示

2016-06-07 13:37 375 查看
#!/usr/bin/env python

#_*_ coding:utf-8 _*_

user_file = 'userlist.txt'

info = raw_input('Input the info to search: ')

f = file(user_file,'rb')

count = 0

for line  in f.xreadlines():

    line = line.strip('\n').split()

    if info in line:

        line = ["\033[31m %s \033[0m" % info if i == info else i for i in line]

        for i in line:

            print i,

        print '\n'

        count += 1

if count == 0:

    print 'the info is not exists in your txt'

else:

    print 'match %d lines'%(count)

f.close()

~        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: