PYthon日志查询

#!/usr/bin/python
#-*- coding: UTF-8 -*-

import os, sys, string, codecs


# 设置字符集
reload(sys)
sys.setdefaultencoding("utf-8")

# 将比赛ID, 玩家id加入
def AddMapKeySetVal(Map, Key, Val):
    if Map.has_key(Key):
        Map[Key].add(Val)
    else:
        Map[Key] = set()
        Map[Key].add(Val)


def RemoveOneVal(Map, Key, Val):
    if Map.has_key(Key):
        Map[Key].discard(Val)


def RemoveKey(Map, Key):
    if Map.has_key(Key):
        del Map[Key]

def PrintMap(file, Map):
    count = 0
    line = ""
    userDic = {};
    userList = [];
    for (Key, Val) in Map.items():
        for userid in Val:
            line += "%s\n" % userid
            count += 1;
            if count%100 == 0:
                #print line
                file.write(line)
                line = ""
    file.write(line)



# 获取长字符串中,两个字符串中间的值
def getValue(srcStr, destStr1, destStr2):
    start = srcStr.find(destStr1) + len(destStr1)
    end = -1
    if destStr2 != "":
        end = srcStr.find(destStr2)
    return srcStr[start:end]

#处理matchserverlog
def handleMatchserverLog(fileName):
    matchDic = {}
    file = open(fileName)
    for line in file:
        # 处理用户加入的比赛
        if line.find("user join match delcard, cardCount") > -1:
            matchId = getValue(line, "matchId:", ", userId:")
            userId  = getValue(line, " userId:", "")
            AddMapKeySetVal(matchDic,matchId,userId)
        if line.find("user quit match addcard, cardCount") > -1:
            matchId = getValue(line, " matchId:", ", userId:")
            userId = getValue(line, " userId:", "")
            RemoveOneVal(matchDic, matchId, userId)
        if line.find("FinalRankingInfo matchID") > -1:
            matchId = getValue(line, " matchID:", "    Order:")
            RemoveKey(matchDic, matchId)

    output = open('AddCardUserd_new.txt','a')
    PrintMap(output,matchDic)
    #print matchDic

fileName = "2017-08-10_MatchServer(1).log"
handleMatchserverLog(fileName)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值