python re正则表达式实例2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Filename: file_process_insert.py
#function:
#author: 刘红波
#date: 2012-05-07
import glob
import os
import re
#目标文件 tab_into_insert.txt
#f_tab1 = open('G://tmp//py//tab_into_190_insert.txt','r')
#f_tab2 = open('G://tmp//py//tab_into_190_insert_new.txt','w')
f_tab1 = open('G://tmp//py//tab_all.txt','r')
f_tab2 = open('D://tab_all_new.txt','w')
plog = re.compile('_[/d]{8}/.log')
pinsert = re.compile('INSERT[/s]+INTO[/s]')
pdate = re.compile('[/d]{8}')
pmonth = re.compile('[/d]{6}')
for line in f_tab1:
line = plog.sub('.tcl',line)
line = pinsert.sub('',line)
line = pdate.sub('YYYYMMDD',line)
line = pmonth.sub('YYYYMM',line)
f_tab2.write(line)
f_tab1.close()
f_tab2.close()
摘自 滴水穿石