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;

原文地址:https://www.cnblogs.com/cyf1995/p/6813088.html