python 文本查找

 

1.文本查找

——正则表达式法:

import re
f = open('1.txt')
source = f.read()
f.close()
r = r'hello .*'
s = re.findall(r,source)
print s

 

ref:http://www.runoob.com/python/python-reg-expressions.html

  • r = r'hello (.*)' :
import re
f = open('1.txt')
source = f.read()
f.close()
r = r'(.*?) hello (.*)'
s = re.findall(r,source)
print s

 

 2.格式化输出

# -*- coding: utf-8 -*-
import re

f = open('vanet.out')
source = f.read()
f.close()
r = r'node (.*)]:At time (.*)s on-off application sent 64 bytes to 10.0.0.(.*)'
s = re.findall(r,source)

for out in s:
    print out

——json

import re
import json;

f = open('1.txt')
source = f.read()
f.close()
r = r'(.*?) hello (.*)'
s = re.findall(r,source)
print s

out = json.dumps(s,indent=0);
print "s=",out;

 

posted on 2017-05-05 15:06  沉郁风  阅读(1774)  评论(0编辑  收藏  举报

导航