Python-キーワードのキャプチャを実現

2463 ワード

#-- coding: utf-8 -- # use utf8 coding
#File : id
#Author : ryan
#Version: Revision
#CVS server will extern the mark id & Revision to proper string
import re import os import sys import csv
#print sys.getdefaultencoding()
reload(sys) sys.setdefaultencoding(‘utf8’) stage_str=r'転送t収集+転送t前処理tフィーチャー抽出tマッチング'stage_dics = [ ( ‘TRANSPORT’,’[TIME]: use=’ ), ( ‘DATAGET’,’[TIME]: KEY_DOWN->GOT DATA=’ ), ( ‘PREPROCESS’,’[TIME]: preprocess=’ ), ( ‘FEATUREGET’,’ALGO Verify: [getFeature]=’ ), ( ‘FEATUREIDENTIFY’,’ALGO Verify: [FeatureRecognition]=’ ), #( ‘TEST’, ‘use=’), #( ‘TEST1’, “KEY_DOWN->GOT DATA”) ] def dics_print(dics): for v in dics: print v
def usage(): print “”” Usage: logparser filename Discription: logparser - this command filename - the log file to be parsed output - data table “”“
def retrieve_lines(log_dir, dics): # lines = [] str = ” statistics = [stage_str,] pattern = re.compile(r”=\d*”)
try:
    logfile = open(log_dir, "r")
except IOError:
    print >> sys.stderr, "File could not be opened!"
    sys.exit(1)

records = logfile.readlines()
for record in records:
    for keys in dics:
        if re.search(keys[1], record) is not None:
            #                lines.append(record)
            tstr = pattern.findall(record)
            #print str
            str += tstr[0][1:]
            str += '\t'
            if keys[0] == dics[-1][0]:
                statistics.append(str)
                str = ''

# print str
# str += ‘’
# statistics.append(str)
logfile.close()
return statistics

def print_lines(lines): for line in lines: print(line)
def write_to_csv(csvfile, lines): csvf = file(csvfile, ‘w+’) writer = csv.writer(csvf) for line in lines: # print line writer.writerow(line.split(‘\t’)) csvf.close()
def calc_data(csvfile): csvf = file(csvfile,’rb’) csvcontent = csv.reader(csvf) res = ” for row in csvcontent: res = str(row) tra,sam,pre,fea,rec,sum = res.split(‘,’) print tra
csvf.close()

if name == ‘main‘: # dics_print(stage_dics) if len(sys.argv) != 2: usage() sys.exit(1) ret_lines = retrieve_lines( sys.argv[1], stage_dics)
# print_lines(ret_lines)
write_to_csv('result.csv', ret_lines)
calc_data('result.csv')