python将日志导入数据库代码案例

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

(1)

#!/usr/bin/env python

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


import MySQLdb

import os, sys, re,string

import time, tarfile,getopt


optmap = {

                'dbuser' : 'haoren',

                'dbpass' : '123654',

                'dbhost' : '192.168.10.12',

                'dbport' : 3306,

                'dbname' : 'JIESUANDB'

                 }

def get_files(dir, pattern):

        res_file_list =[] #定义一个空列表来存储

        if os.path.exists(dir):

                cur_file_list = os.listdir(dir)

                for file_name in cur_file_list:

                        if re.search(pattern, file_name):

                                res_file_list.append(file_name)

                return res_file_list

        else:

                return 'no'

def main():


        cur_day = time.strftime("%Y%m%d", time.localtime(time.time()-86400))


        opts, args = getopt.getopt(sys.argv[1:], 'd:')


        for op, value in opts:

                if op == '-d':

                        m = re.search('[0-9]{8}', value)

                        if m:

                                cur_day = value

                        else:

                                print "请输入8位日期(比如:20130215)"

                                return 'no'


log_day = time.strftime('%y%m%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))


fmt_day = time.strftime('%Y-%m-%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))


        print '结算统计日期:',fmt_day


        #log_day = time.strftime("%y%m%d", time.localtime(time.time()-86400))

        dirname="/home/haoren/logdir/%s_67"%log_day

        print dirname



        db_conn = MySQLdb.connect(user=optmap['dbuser'], passwd=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], db=optmap['dbname'])

        db_cursor=db_conn.cursor()

        db_conn.query("use %s"%optmap['dbname'])


 tabletime = time.strftime("%y%m%d", time.localtime(time.mktime(time.strptime(cur_day, "%Y%m%d"))))


   sql="CREATE TABLE IF NOT EXISTS `DAOJUTONGJI_%s` like DAOJUTONGJISAMPLE"%tabletime


        db_conn.query(sql)

        db_conn.query("delete from DAOJUTONGJI_%s"%tabletime)


        if os.path.exists("/tmp/DJTongJi20161226.txt"):

                os.system("rm -f /tmp/DJTongJi20161226.txt")



        file_list2=get_files(dirname,'biserver')#调用前面的函数

        for file2 in file_list2:

                command = "cat %s/%s | grep -h -w  消耗人民币 |grep -v 人民币消费道具统计 |grep 频道类型  >> /tmp/DJTongJi20161226.txt"%(dirname,file2)

                os.system(command)


        #结算统计记录放在txt文档里面

        filename='/tmp/DaoJuTongJi20161226.txt'

        record = {}

        a_file = open(filename, 'r')

        #161226-16:23:06 Bill[40268]  INFO: [平台统计]频道(3839247)等级(2)用户(60303642)赠送(10)个道具(95)给消费者(82147138),消费者等级(38)签约(1)消耗人民币(100)中介获得人民币(8000)平台获得人民币(2000)当前礼物最大值(52000)当前平台(4023568)平台类型(150)

        for a_line in a_file.readlines():

                                               m = re.search("^(\S+) Bill\[\d+\]  INFO: \[平台统计\]频道\((\d+)\)等级\((\d+)\)用户\((\S+)\)赠送\((\d+)\)个道具\((\d+)\)给歌唱家\((\d+)\),中介等级\((\d+)\)签约\((\d+)\)消耗人民币\((\d+)\)歌唱家获得人民币\((\d+)\)平台获得人民币\((\d+)\)当前礼物最大值\((\d+)\)当前平台\((\d+)\)平台类型\((\d+)\)", a_line)

                        if m:

                          #print "第一项:"+m.group(1)

                          #print "第二项:"+m.group(2)

                          #print "第三项:"+m.group(3)

                          #print "第四项:"+m.group(4)

                          #print "第五项:"+m.group(5)

                          #print "第六项:"+m.group(6)

                          #print "第七项:"+m.group(7)

                          #print "第八项:"+m.group(8)

                          #print "第九项:"+m.group(9)

                          #print "第十项:"+m.group(10)

                          #print "第十一项:"+m.group(11)

                          #print "第十二项:"+m.group(12)

                          #print "第十三项:"+m.group(13)

                          #print "第十四项:"+m.group(14)

                          #print "第十五项:"+m.group(15)

                                #if int(m.group(14)) >0 or int(m.group(15)) >0 :

                          db_conn.query("insert into DAOJUTONGJI_%s(CHANNELID,CHANELLEVEL,PRESENTERID,PRESENTERNUM,ITEMID,SINGERID,SINGERLEVEL,SIGNTYPE,CONSUMECOIN,SINGERRECVGOLD,CHANNELRECVGOLD,MAXVALUE,CURRENTCHANNEL,CHANNELTYPE) values(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)"%(tabletime,int(m.group(2)),int(m.group(3)),int(m.group(4)),int(m.group(5)),int(m.group(6)),int(m.group(7)),int(m.group(8)),int(m.group(9)),int(m.group(10)),int(m.group(11)),int(m.group(12)),int(m.group(13)),int(m.group(14)),int(m.group(15))))


        a_file.close()

        db_conn.commit()

        db_cursor.close()

        db_conn.close()

main()

#if __name__ == "__main__":

#        main()













#!/usr/bin/env python

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

#歌唱家收入日报-合并物品

#****************************************使用说明****************************************************

# 内网测试通过命令行参数

# -d 指定统计时间

# -p 指定日志所在目录

# -t 指定临时文件存放目录

# 示例如下:

# [ying@localhost tongji]$ ./singerincomeDaily.py -d 20141112 -p /home/ying/log/ -t /home/ying/tongji/

# 外网无需加参数,使用默认值

#****************************************使用说明****************************************************


import MySQLdb

import os, sys, re,string

import time, tarfile,getopt


optmap = {

                'dbuser' : 'haoren',

                'dbpass' : '123654',

                'dbhost' : '192.168.10.13',

                'dbport' : 3306,

                'dbname' : 'GHMKDB'

        }

def get_files(dir, pattern):

        res_file_list =[]

        if os.path.exists(dir):

                cur_file_list = os.listdir(dir)

                cur_file_list.sort()

                for file_name in cur_file_list:

                        if re.search(pattern, file_name):

                                res_file_list.append(file_name)

                return res_file_list

        else:

                return 'no'


def main():


        #外网环境默认参数

        cur_day = time.strftime("%Y%m%d", time.localtime(time.time()-86400))

        log_day = time.strftime('%y%m%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

        logdirname = "/home/haoren/logdir/%s_67"%log_day

        tmpdirname = "/tmp/"

        logname = "billserver"


        #内网测试指定参数

        opts, args = getopt.getopt(sys.argv[1:], 'd:p:t:')

        for op, value in opts:

                if op == '-d':

                        m = re.search('[0-9]{8}', value)

                        if m:

                                cur_day = value

                                log_suffix = time.strftime('%y%m%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

                                logname = "billserver.log"

                                log_day = time.strftime('%y%m%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

                                logdirname = "/home/haoren/logdir/%s_67"%log_day


                        else:

                                print "请输入8位日期(比如:20130215)"

                                return 'no'

                elif op == '-p':

                        logdirname = str(value)

                elif op == '-t':

                        tmpdirname = str(value)


        fmt_day = time.strftime('%Y-%m-%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

        d_time = time.mktime(time.strptime(fmt_day,'%Y-%m-%d'))

        print '统计日期:',fmt_day

        print '日志名称:',logname

        print '日志路径:',logdirname

        print '临时文本:',tmpdirname


        #歌唱家收入

        if os.path.exists("%ssingerincome.txt"%tmpdirname):

                os.system("rm -f %ssingerincome.txt"%tmpdirname)


        #歌唱家人民币总量

        if os.path.exists("%ssingerallgold.txt"%tmpdirname):

                os.system("rm -f %ssingerallgold.txt"%tmpdirname)


        file_list2=get_files(logdirname, logname)

        for file2 in file_list2:

                command = "cat %s/%s | awk '/物品统计/'>> %ssingerincome.txt"%(logdirname,file2,tmpdirname)

                os.system(command)

                command = "cat %s/%s | awk '/用户人民币统计/'>> %ssingerallgold.txt"%(logdirname,file2,tmpdirname)

                os.system(command)


        #歌唱家收入

        filename='%ssingerincome.txt'%tmpdirname

        a_file = open(filename, 'r')

        #歌唱家消耗用户总人民币

        allcoin={}

        #歌唱家人民币收入

        goldincome={}

        #歌唱家总人民币

        singerallgold = {}

        #歌唱家物品

        singerprops = {}

        #特殊物品数目统计

        propsnum = {}

        #130815-15:01:06 Bill[990]      INFO: [物品统计]平台(210043)等级(9)用户(21000162)赠送(1)个物品(3)给歌唱家(21000264),歌唱家等级(1),签约(0), 消耗人民币(10), 歌唱家获得人民币(470), 平台获得人民币(30)

        #161226-14:41:32 Bill[40268]  INFO: [物品统计]平台(4037375)等级(1)用户(83272744)赠送(1)个物品(3)给歌唱家(85925548),歌唱家等级(1)签约(1)消耗人民币(10)歌唱家获得人民币(800)平台获得人民币(200)当前礼物最大值(13140)当前平台(85925548)平台类型(187)

        for a_line in a_file.readlines():

                #m = re.search("^(\S+) Bill\[\d+\]  INFO: \[物品统计\]平台\((\d+)\)等级\((\d+)\)用户\((\d+)\)赠送\((\d+)\)个物品\((\d+)\)给歌唱家\((\d+)\),歌唱家等级\((\d+)\),签约\((\d+)\), 消耗人民币\((\d+)\), 歌唱家获得人民币\((\d+)\), 平台获得人民币\((\d+)\)", a_line)

                m = re.search("^(\S+) Bill\[\d+\]  INFO: \[物品统计\]平台\((\d+)\)等级\((\d+)\)用户\((\d+)\)赠送\((\d+)\)个物品\((\d+)\)给歌唱家\((\d+)\),歌唱家等级\((\d+)\)签约\((\d+)\)消耗人民币\((\d+)\)歌唱家获得人民币\((\d+)\)平台获得人民币\((\d+)\)当前礼物最大值\((\d+)\)当前平台\((\d+)\)平台类型\((\d+)\)", a_line)

                if m:

                        propnum = int(m.group(5))

                        propid = int(m.group(6))

                        singerid = int(m.group(7))

                        signflag = int(m.group(9))

                        coin = int(m.group(10))

                        gold = int(m.group(11))

                        #离线用户signflag传入的是平台号

                        if (signflag != 0 and signflag != 1):

                                if (signflag == int(m.group(2))):

                                        signflag = 1

                                else:

                                        signflag = 0


                        if(singerid in singerprops):

                                if (propid in singerprops[singerid]):

                                        singerprops[singerid][propid] += propnum

                                        allcoin[singerid][propid] += coin

                                        goldincome[singerid][propid][signflag] += gold

                                else:

                                        singerprops[singerid][propid] = propnum

                                        allcoin[singerid][propid] = coin

                                        goldincome[singerid][propid] = {}

                                        goldincome[singerid][propid][0] = 0

                                        goldincome[singerid][propid][1] = 0

                                        goldincome[singerid][propid][signflag] = gold


                        else:

                                singerprops[singerid]={}

                                singerprops[singerid][propid] = propnum

                                allcoin[singerid] = {}

                                allcoin[singerid][propid] = coin

                                goldincome[singerid] = {}

                                goldincome[singerid][propid] = {}

                                goldincome[singerid][propid][0] = 0

                                goldincome[singerid][propid][1] = 0

                                goldincome[singerid][propid][signflag] = gold


                        if (propid in propsnum):

                                if (propnum in [10,30,66,188,520,1314]):

                                        propsnum[propid][propnum] += 1;

                        else:

                                propsnum[propid]={}

                                propsnum[propid][10] = 0

                                propsnum[propid][30] = 0

                                propsnum[propid][66] = 0

                                propsnum[propid][188] = 0

                                propsnum[propid][520] = 0

                                propsnum[propid][1314] = 0

                                if (propnum in [10,30,66,188,520,1314]):

                                        propsnum[propid][propnum] += 1;


        for key in singerprops:

                print '歌唱家:',key


#       for key in propsnum:

#               print '物品:',key

#               print '10:',propsnum[key][10]

#               print '30:',propsnum[key][30]

#               print '66:',propsnum[key][66]

#               print '188:',propsnum[key][188]

#               print '520:',propsnum[key][520]

#               print '1314:',propsnum[key][1314]


        a_file.close()


        #歌唱家总人民币统计


        #歌唱家总人民币

        filename='%ssingerallgold.txt'%tmpdirname

        a_file = open(filename, 'r')

        #歌唱家总人民币

        allgold={}

        #Zebra::logger->info("[用户统计]用户(%u)当前人民币(%u)",)

        for a_line in a_file.readlines():

                m = re.search("^(\S+) Bill\[\d+\]  INFO: \[用户人民币统计\]用户\((\d+)\)当前人民币\((\d+)\)", a_line)

                if m:

                        allgold[int(m.group(2))] = int(m.group(3))


        for key in allgold:

                print'歌唱家:%u,人民币总量:%u'%(key,allgold[key])


        a_file.close()


        db_conn = MySQLdb.connect(user=optmap['dbuser'], passwd=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], db=optmap['dbname'])

        db_cursor=db_conn.cursor()

        db_conn.query("use %s"%optmap['dbname'])

        tabletime = time.strftime("%Y%m%d", time.localtime(time.mktime(time.strptime(cur_day, "%Y%m%d"))))


        sql="CREATE TABLE IF NOT EXISTS `SINGEREARNINGDAILY_%s` like SINGEREARNINGDAILY_SAMPLE"%tabletime


        db_conn.query(sql)

        db_conn.query("delete from SINGEREARNINGDAILY_%s"%tabletime)

        for key in singerprops:

                if(key not in allgold):

                        allgold[key]=0

                for kitem in singerprops[key]:

                        db_conn.query("insert into SINGEREARNINGDAILY_%s(USERID,RECVITEMTOTALCOIN,SIGNGOLD,NONSIGNGOLD,CURGOLD,ITEMID,ITEMNUM) values(%d,%d,%d,%d,%d,%d,%d)"%(tabletime,int(key),int(allcoin[key][kitem]),int(goldincome[key][kitem][1]),int(goldincome[key][kitem][0]),int(allgold[key]),int(kitem),int(singerprops[key][kitem])))


        db_conn.query("delete from ITEMSPECIALNUMMONTHLY where TIME=%d"%d_time)


        for key in propsnum:

                db_conn.query("insert into ITEMSPECIALNUMMONTHLY (TIME,ITEMID,NUM10,NUM30,NUM66,NUM188,NUM520,NUM1314) values(%d,%d,%d,%d,%d,%d,%d,%d)"%(d_time,key,propsnum[key][10],propsnum[key][30],propsnum[key][66],propsnum[key][188],propsnum[key][520],propsnum[key][1314]))



        db_conn.commit()

        db_cursor.close()

        db_conn.close()

main()

#if __name__ == "__main__":

#                main()




#!/usr/bin/env python

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

#礼物转盘日报

#****************************************使用说明****************************************************

# 内网测试通过命令行参数

# -d 指定统计时间

# 示例如下:

# [ying@localhost tongji]$ ./giftrollincomemonthly.py -d 20141112

# 外网无需加参数,使用默认值

#****************************************使用说明****************************************************


import MySQLdb

import os, sys, re,string

import time, tarfile,getopt

#import argparse


optmap = {

                'dbuser' : 'haoren',

                'dbpass' : '123654',

                'dbhost' : '192.168.10.13',

                'dbport' : 3306,

                'dbname' : 'GHKMDB'

        }

def get_files(dir, pattern):

        res_file_list =[]

        if os.path.exists(dir):

                cur_file_list = os.listdir(dir)

                cur_file_list.sort()

                for file_name in cur_file_list:

                        if re.search(pattern, file_name):

                                res_file_list.append(file_name)

                return res_file_list

        else:

                return 'no'


def main():


        #外网环境默认参数

        cur_day = time.strftime("%Y%m%d", time.localtime(time.time()-86400))

        tmpdirname = "/tmp/"

        logname = "billserver"


        #内网测试指定参数

        opts, args = getopt.getopt(sys.argv[1:], 'd:p:t:')

        for op, value in opts:

                if op == '-d':

                        m = re.search('[0-9]{8}', value)

                        if m:

                                cur_day = value

                                log_suffix = time.strftime('%y%m%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

                                logname = "billserver.log"


                        else:

                                print "请输入8位日期(比如:20130215)"

                                return 'no'


        log_day = time.strftime('%y%m%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

        logdirname = "/home/haoren/logdir/%s_67"%log_day


        fmt_day = time.strftime('%Y-%m-%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

        d_time = time.mktime(time.strptime(fmt_day,'%Y-%m-%d'))

        print '统计日期:',fmt_day

        print '日志名称:',logname

        print '日志路径:',logdirname

        print '临时文本:',tmpdirname


        #礼物转盘收入

        if os.path.exists("%sgiftrollincome.txt"%tmpdirname):

                os.system("rm -f %sgiftrollincome.txt"%tmpdirname)

        if os.path.exists("%sgiftrolltrycount.txt"%tmpdirname):

                os.system("rm -f %sgiftrolltrycount.txt"%tmpdirname)

        if os.path.exists("%sgiftrollnum.txt"%tmpdirname):

                os.system("rm -f %sgiftrollnum.txt"%tmpdirname)

        file_list1=get_files(logdirname, logname)

        for file1 in file_list1:

                command = "cat %s/%s | awk '/通用人民币操作/' | awk '/成功余额/' >> %sgiftrollincome.txt"%(logdirname,file1,tmpdirname)

                os.system(command)

                #command1 = "cat %s/%s | awk '/tryCount/' >> %sgiftrolltrycount.txt"%(logdirname,file1,tmpdirname)

                #os.system(command1)

                #command2 = "cat %s/%s | awk '/selected item id/' >> %sgiftrollnum.txt"%(logdirname,file1,tmpdirname)

                #os.system(command2)


        #收入

        filename='%sgiftrollincome.txt'%tmpdirname

        a_file = open(filename, 'r')

        #消耗用户总人民币

        allcoin = 0

        #用户赢取人民币

        playerwin = 0

        #参与人数 

        playernum = 0

        #机器人数

        robotnum = 0

        #下注次数

        betnum = 0

        #中奖次数

        winnum = 0

        #参与用户

        userlist = []


        for a_line in a_file.readlines():

                #150618-15:59:16 Bill[40268]  INFO: [通用人民币操作]用户(53730047)扣除人民币(100)成功余额(3920)操作类型(2)操作序号(69954)

                m = re.search("^(\S+) Bill\[\d+\]  INFO: \[通用人民币操作]用户\((\d+)\)扣除人民币\((\d+)\)成功余额\((\d+)\)操作类型\((\d+)\)操作序号\((\d+)\)", a_line)

                if m:

                        if int(m.group(5)) == 2 : 

                                allcoin += int(m.group(3))

                                betnum += 1

                                userid = int(m.group(2))

                                #参与用户列表

                                if (userid not in userlist):

                                        userlist.append(userid)

                                        playernum += 1


                #150618-15:59:01 Bill[40268]  INFO: [通用人民币操作]用户(23119814)增加人民币(5500)成功余额(18700)操作类型(2)操作序号(69905)

                m1 = re.search("^(\S+) Bill\[\d+\]  INFO: \[通用人民币操作]用户\((\d+)\)增加人民币\((\d+)\)成功余额\((\d+)\)操作类型\((\d+)\)操作序号\((\d+)\)", a_line)

                if m1:

                        if int(m1.group(5)) == 2 :

                                playerwin += int(m1.group(3))

                                winnum += 1


        print '总金额(人民币):',allcoin/100

        print '用户赢的人民币:',playerwin/100

        print '官方赢的人民币:',(allcoin-playerwin)/100

        print '参与人数:',playernum

        print '下注次数:',betnum

        print '中奖次数:',winnum


        a_file.close()


        logname_gameapp = "gameappserver"

        logdirname_gameapp = "/home/haoren/logdir/%s_34"%log_day


        if os.path.exists("%sgiftrolltrycount.txt"%tmpdirname):

                os.system("rm -f %sgiftrolltrycount.txt"%tmpdirname)

        if os.path.exists("%sgiftrollnum.txt"%tmpdirname):

                os.system("rm -f %sgiftrollnum.txt"%tmpdirname)

        if os.path.exists("%sgiftrolltrycountsum.txt"%tmpdirname):

                os.system("rm -f %sgiftrolltrycountsum.txt"%tmpdirname)


        file_list2=get_files(logdirname_gameapp, logname_gameapp)

        for file2 in file_list2:

                #command1 = "cat %s/%s | awk '/tryCount/' >> %sgiftrolltrycount.txt"%(logdirname_gameapp,file2,tmpdirname)

                command1 = "cat %s/%s | awk '/GiftRoll2/'| awk '/GlobalSummary/' >> %sgiftrolltrycount.txt"%(logdirname_gameapp,file2,tmpdirname)

                os.system(command1)

                command2 = "cat %s/%s | awk '/GiftRoll2/'| awk '/GlobalSummary/'| awk '/selectedItem/' >> %sgiftrollnum.txt"%(logdirname_gameapp,file2,tmpdirname)

                os.system(command2)


        filename1='%sgiftrolltrycount.txt'%tmpdirname

        filename2='%sgiftrollnum.txt'%tmpdirname

        filename3='%sgiftrolltrycountsum.txt'%tmpdirname


        trycountlinenum = sum(1 for line in open(filename1))

        rollnum = sum(1 for line in open(filename2))

        print 'TRYCOUNTLINENUM:',trycountlinenum

        print 'ROLLNUM:',rollnum


        command3 = "cat %s |awk -F '|' '{print $8}'|awk -F '=' 'BEGIN{tsum=0}{if($2!=0) tsum+=1} END{print tsum}' >> %sgiftrolltrycountsum.txt"%(filename1,tmpdirname)

        os.system(command3)

        a_file3 = open(filename3, 'r')

        trycount = 0

        for line3 in a_file3.readlines():

                trycount = int(line3.strip())

        #trycount = int(trycountsum) - int(trycountlinenum)

        print 'TRYCOUNT:',trycount


        db_conn = MySQLdb.connect(user=optmap['dbuser'], passwd=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], db=optmap['dbname'])

        db_cursor=db_conn.cursor()

        db_conn.query("use %s"%optmap['dbname'])


        d_time = time.mktime(time.strptime(cur_day,'%Y%m%d'))

        db_conn.query("delete from GIFTROLL2INCOMEMONTHLY where TIME=%u"%d_time)

        db_conn.query("insert into GIFTROLL2INCOMEMONTHLY(TIME,TOTALCOIN,PLAYERWINCOIN,PLAYERNUM,BETNUM,LUCKYNUM,TRYCOUNT,ROLLNUM) values(%d,%d,%d,%d,%d,%d,%d,%d)"%(d_time, allcoin, playerwin, playernum, betnum, winnum,trycount,rollnum))


        db_conn.commit()

        db_cursor.close()

        db_conn.close()

main()

#if __name__ == "__main__":

#                main()



















#!/usr/bin/env python

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

#特殊鲜花明细


import MySQLdb

import os, sys, re,string

import time, tarfile,getopt


optmap = {

                'dbuser' : 'haoren',

                'dbpass' : '123654',

                'dbhost' : '192.168.10.12',

                'dbport' : 3306,

                'dbname' : 'JIESUANHBDB'

                 }

def get_files(dir, pattern):

        res_file_list =[]

        if os.path.exists(dir):

                cur_file_list = os.listdir(dir)

                for file_name in cur_file_list:

                        if re.search(pattern, file_name):

                                res_file_list.append(file_name)

                return res_file_list

        else:

                return 'no'

def main():


        cur_day = time.strftime("%Y%m%d", time.localtime(time.time()-86400))


        opts, args = getopt.getopt(sys.argv[1:], 'd:')

        for op, value in opts:

                if op == '-d':

                        m = re.search('[0-9]{8}', value)

                        if m:

                                cur_day = value

                        else:

                                print "请输入8位日期(比如:20130215)"

                                return 'no'


        log_day = time.strftime('%y%m%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))


        fmt_day = time.strftime('%Y-%m-%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

        print '结算统计日期:',fmt_day


        #log_day = time.strftime("%y%m%d", time.localtime(time.time()-86400))

        dirname="/home/haoren/logdir/%s_67"%log_day

        print dirname



        db_conn = MySQLdb.connect(user=optmap['dbuser'], passwd=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], db=optmap['dbname'])

        db_cursor=db_conn.cursor()

        db_conn.query("use %s"%optmap['dbname'])

        tabletime = time.strftime("%y%m%d", time.localtime(time.mktime(time.strptime(cur_day, "%Y%m%d"))))


        sql="CREATE TABLE IF NOT EXISTS `DUBIJIESUANTONGJI_%s` like DUBIJIESUANTONGJISAMPLE"%tabletime


        db_conn.query(sql)

        db_conn.query("delete from DUBIJIESUANTONGJI_%s"%tabletime)


        if os.path.exists("/tmp/DuBiJieSuanTongJi2016.txt"):

                os.system("rm -f /tmp/DuBiJieSuanTongJi2016.txt")



        file_list2=get_files(dirname,'billserver')

        for file2 in file_list2:

                command = "cat %s/%s | grep -h -w  人民币结算 |grep 原来人民币  >> /tmp/DuBiJieSuanTongJi2016.txt"%(dirname,file2)

                os.system(command)


        #结算统计记录放在txt文档里面

        filename='/tmp/DuBiJieSuanTongJi2016.txt'

        record = {}

        a_file = open(filename, 'r')

        #160125-11:00:14 Bill[40268]  INFO: [结算统计]时间(1453690814)类别(1)名称(物品收入)平台(3977962)等级(2)用户(65147500)赠送(1)个物品(39)给歌唱家(65147500),歌唱家等级(28),签约(1), 消耗人民币(100), 歌唱家获得金币(8000), 平台获得人民币(2000),歌唱家当前人民币(1320960)平台当前人民币(335560)

        for a_line in a_file.readlines():

                        m = re.search("^(\S+) Bill\[\d+\]  INFO: \[人民币结算\]时间\((\d+)\),用户ID\((\d+)\),原来人民币\((\d+)\),现在人民币\((\d+)\),人民币操作\((\d+)\),(\S+)\((\d+)\),操作类型\((\d+)\),操作详情\((\d+)\),操作数量\((\d+)\).描述:(\S+)", a_line)

                        if m:

                            print "第一项:"+m.group(1)

                            print "第二项:"+m.group(2)

                            print "第三项:"+m.group(3)

                            print "第四项:"+m.group(4)

                            print "第五项:"+m.group(5)

                            print "第六项:"+m.group(6)

                            print "第七项:"+m.group(7)

                            print "第八项:"+m.group(8)

                            print "第九项:"+m.group(9)

                            print "第十项:"+m.group(10)

                            print "第十一项:"+m.group(11)

                            print "第十二项:"+m.group(12)

                            #print "第十三项:"+m.group(13)

                            #print "第十四项:"+m.group(14)

                            #print "第十五项:"+m.group(15)

                            #print "第十六项:"+m.group(16)

                            #print "第十七项:"+m.group(17)

                            #print "第十八项:"+m.group(18)

                            db_conn.query("insert into DUBIJIESUANTONGJI_%s(OPTIME,CONSUMERID,PRESERVENUM,CURRENTNUM,DUBIOPTYPE,DUBIOPNUM,OPTYPE,OPDETAIL,OPNUM,NAME) values(%d,%d,%d,%d,%d,%d,%d,%d,%d,'%s')"%(tabletime,int(m.group(2)),int(m.group(3)),int(m.group(4)),int(m.group(5)),int(m.group(6)),int(m.group(8)),int(m.group(9)),int(m.group(10)),int(m.group(11)),str(m.group(12))))

                            #print  'insert into DUBIJIESUANTONGJI_%s(OPTIME,CONSUMERID,PRESERVENUM,CURRENTNUM,DUBIOPTYPE,DUBIOPNUM,OPTYPE,OPDETAIL,OPNUM,NAME) values(%d,%d,%d,%d,%d,%d,%d,%d,%d,%s)'%(tabletime,int(m.group(2)),int(m.group(3)),int(m.group(4)),int(m.group(5)),int(m.group(6)),int(m.group(7)),int(m.group(8)),int(m.group(9)),int(m.group(10)),str(m.group(11)))


        a_file.close()

        #db_conn.commit()

        #db_cursor.close()

        db_conn.close()

main()

#if __name__ == "__main__":

        #main()











本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1895778,如需转载请自行联系原作者
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
1天前
|
SQL 关系型数据库 MySQL
第十三章 Python数据库编程
第十三章 Python数据库编程
|
1天前
|
JSON Shell 数据格式
第十章 Python常用标准库使用(必会)
第十章 Python常用标准库使用(必会)
|
2天前
|
开发框架 前端开发 数据库
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
|
2天前
|
数据采集 数据可视化 数据处理
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
|
2天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
2天前
|
JSON 测试技术 API
Python的Api自动化测试使用HTTP客户端库发送请求
【4月更文挑战第18天】在Python中进行HTTP请求和API自动化测试有多个库可选:1) `requests`是最流行的选择,支持多种请求方法和内置JSON解析;2) `http.client`是标准库的一部分,适合需要低级别控制的用户;3) `urllib`提供URL操作,适用于复杂请求;4) `httpx`拥有类似`requests`的API,提供现代特性和异步支持。根据具体需求选择,如多数情况`requests`已足够。
8 3
|
2天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
20 0
|
2天前
|
人工智能 Python
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
6 0
Python
19 0
|
2天前
|
JSON 数据格式 索引
python 又一个点运算符操作的字典库:Munch
python 又一个点运算符操作的字典库:Munch
19 0

热门文章

最新文章