【python】日志处理代码

# -*- coding: utf-8 -*-

import xlwt
import sys
reload(sys)
sys.setdefaultencoding('gbk')

class YomiAnalyser:

    def __init__(self, data_path):
        self.data_path = data_path
        self.data_generater = self.__generateData()

    def __generateData(self):
        with open(self.data_path, "r") as rf:
            for line in rf:
                line = unicode(line, "gbk")
                line = line.split("\t")
                line = [l.strip() for l in line]
                yield line

    # 字段筛选
    def filterField(self, field_index, field_value):
        filter_data = []
        while True:
            try:
                data = next(self.data_generater)
                if data[field_index] == field_value:
                    filter_data.append(data)
            except StopIteration as e:
                # print("Generator return value: " , e.value)
                break
        return filter_data

    # 统计参数
    def sumupField(self, data, sum_index):
        sum_result = {}
        for line in data:
            if line[sum_index] not in sum_result:
                sum_result[line[sum_index]] = 1
            else:
                sum_result[line[sum_index]] += 1

        sorted_key_list = sorted(sum_result, key = lambda x:sum_result[x], reverse=True)
        sorted_result = map(lambda x:{x:sum_result[x]}, sorted_key_list)
        sorted_result = list(sorted_result)
        return sorted_result
        
    
if __name__ == "__main__":
    data_path = "./yomiLog_preprocess.txt"
    ya = YomiAnalyser(data_path)

    # 1.更改过滤动作
    # 2.更改统计字段
    # 3.结果转移

    # 过滤动作
    filter_data = ya.filterField(3, "6")
    # 统计字段
    sorted_result = ya.sumupField(filter_data, 6)

    workbook = xlwt.Workbook(encoding = 'utf-8')
    worksheet = workbook.add_sheet('Sheet0')
    # 前10名
    for i in range(0, min(len(sorted_result), 10)):
        print "-"*100
        print sorted_result[i].values()[0], sorted_result[i].keys()[0]
        worksheet.write(i, 0, sorted_result[i].keys()[0])
        worksheet.write(i, 1, sorted_result[i].values()[0])
    print "-"*100
    
    workbook.save('ResultTemp.xls')

你可能感兴趣的:(语言:Python)