a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 學院 > 操作系統 > 正文

python將日志導入數據庫代碼案例

2024-06-28 16:00:46
字體:
來源:轉載
供稿:網友

(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()


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久成人国产精品 | 国产1页| 日韩欧美国产精品综合嫩v 狠狠综合久久 | 日韩av一区二区在线观看 | 日韩一区欧美一区 | www.白白色 | 久久久中文字幕 | 欧美日韩亚洲天堂 | 嫩草网站| 久久国产麻豆 | 日韩视频一区二区三区 | 亚洲精品视频在线 | 欧美精品在线一区二区三区 | 欧美在线一区二区三区 | 黄色一级免费看 | 亚洲jizzjizz日本少妇 | 欧美 日韩 中文 | 亚洲经典视频在线观看 | 亚洲第一成年免费网站 | 好姑娘影视在线观看高清 | 欧美高清视频在线观看 | 欧美日韩一区二区三区在线观看 | 羞羞网站在线观看入口免费 | 青青草一区二区 | 米奇狠狠狠狠8877 | 亚洲经典自拍 | 日韩精品一区二区三区在线观看 | 国产成人精品av | 国产在线精品一区二区 | 精品国产一区二区在线 | 亚洲福利免费 | 狠狠爱www人成狠狠爱综合网 | 成人a在线视频免费观看 | 免费黄色福利网站 | 亚洲伊人影院 | 伊人免费视频 | 在线免费观看黄色小视频 | 精品国产乱码久久久久久久 | 亚洲精选一区 | 一区二区免费看 | 色综合久久久久 |