python re正则表达式实例1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Filename: file_process.py
#function:
#author: 刘红波
#date: 2012-05-07
import glob
import os
import re
#日志文件目录
v_log_dir = '/etl/aiinsight/bin/py'
#目标文件
f_tab1 = open('/etl/aiinsight/bin/py/tab_into_tab.txt','w')
f_tab2 = open('/etl/aiinsight/bin/py/tab_into_insert.txt','w')
os.chdir(v_log_dir)
#显示指定类型文件
#查询正则表达式
p1 = re.compile('WWNEW\.[\w]+|DW\.[\w]+|REPORT\.[\w]+|TBOWNER\.[\w]+')
p2 = re.compile('INSERT[\s]+INTO[\s]+WWNEW\.[\w]+|INSERT[\s]+INTO[\s]+DW\.[\w]+|INSERT[\s]+INTO[\s]+REPORT\.[\w]+|INSERT[\s]+INTO[\s]+TBOWNER\.[\w]+')
for v_file in glob.glob('*.log'):
f = open(v_file)
for line in f:
for v_tab in p1.findall(line.upper()):
f_tab1.write(v_file + ',' + v_tab + '\n')
for v_tab in p2.findall(line.upper()):
f_tab2.write(v_file + ',' + v_tab + '\n')
f.close()
f_tab1.close()
f_tab2.close()
摘自 滴水穿石
补充:Web开发 , Python ,