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()
~
#_*_ 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()
~
相关文章推荐
- Python 字符串详解
- Python Dictionary详解
- 自动安装Python第三方库
- python-字符串格式化
- Python实现Hive的UDF
- mac下不能卸载和升级numpy的解决办法
- leetcode 81. Search in Rotated Sorted Array II-旋转数组|二分查找
- 自动获取客户登陆信息(Python)
- python操作日期和时间的方法
- 查找python中函数的网站
- Python格式化输出
- Python join()
- python 实现邮件报警
- Scipy Orth
- [学习笔记]python之re模块-----正则表达式
- Python 常用函数time.strftime()简介
- Python模块之---Pexpect
- python---------打印乘法口诀
- selenium-webdriver(python) (十五) -- 鼠标事件
- python find函数