python 生成excel

[['NCBS', 11, 766005, 766016, 0.9999856399866321], ['LOAN', 2, 106474, 106476, 0.9999812164243586], ['IBPS', 14, 39787, 39801, 0.9996482500439687], ['IBANK', 1, 6415, 6416, 0.9998441396508728]]


# !/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
from datetime import datetime
import cx_Oracle
import os
import xlwt
import sys

reload(sys)
import time

sys.setdefaultencoding('utf-8')
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
import datetime  # 导入日期时间模块

today = datetime.date.today()  # 获得今天的日期
yesterday = today - datetime.timedelta(days=1)
i = 0
def write_data_to_excel(name, result):
    # 将sql作为参数传递调用get_data并将结果赋值给result,(result为一个嵌套元组)
    result = result
    print result
    # 实例化一个Workbook()对象(即excel文件)
    wbk = xlwt.Workbook(encoding='utf-8')
    # 新建一个名为Sheet1的excel sheet。此处的cell_overwrite_ok =True是为了能对同一个单元格重复操作。
    sheet = wbk.add_sheet('Sheet1', cell_overwrite_ok=True)
    # 获取当前日期,得到一个datetime对象如:(2016, 8, 9, 23, 12, 23, 424000)
    #today = datetime.today()
    # 将获取到的datetime对象仅取日期如:2016-8-9
    #today_date = datetime.date(today)
    # 遍历result中的没个元素。
    titlelist=['系统标识','交易失败笔数','交易成功笔数','交易总笔数','交易成功率']
    # for i in xrange(len(titlelist)):
    #     sheet.write(1, i, titlelist[i])
    # wbk.save(name + str(yesterday) + '.xls')
    for i in xrange(len(result)):
        # 对result的每个子元素作遍历,
        if i==0:
            for x in xrange(len(titlelist)):
                sheet.write(0, x, titlelist[x])
        for j in xrange(len(result[i])):
            y=i+1
            # 将每一行的每个元素按行号i,列号j,写入到excel中。
            print result[i][j]
            sheet.write(y, j, result[i][j])
    # 以传递的name+当前日期作为excel名称保存。
    wbk.save(name + str(yesterday) + '.xls')

def get_data():
    applist=['NCBS','LOAN','IBPS','IBANK']
    list = [[] for i in range(len(applist))]
    print list
    i=0
    for x in applist:
        print i
        print x
        conn = cx_Oracle.connect('esbdata/[email protected]/esbdb')
        cur = conn.cursor()
        mysql="select  /*+parallel(t 8)*/ cnt1 as 失败笔数,(cnt2 - cnt1) as 成功笔数,cnt2 as 总笔数 ,(cnt2 - cnt1)/cnt2 as 成功率 from (select (select count(*) \
            from esb2_trans_log t \
            where t.trans_date >= \
              trunc(sysdate) \
              and  t.trans_date <= \
              trunc(sysdate+1) \
              and (t.respmsg like '%%Read timed out%%' or t.respmsg like '%%异常%%' or \
              t.respmsg like '%%超时%%' or t.respmsg like '%%通讯%%失败%%') \
              and t.logicsystem='%s' \
                 and t.esbserviceflowno<>'000000' \
             and t.flowstepid in ('4','E') ) cnt1 , \
             ( select  /*+parallel(t 8)*/  count(*) \
             from esb2_trans_log t \
             where  t.trans_date >= \
             trunc(sysdate) \
             and  t.trans_date <= \
             trunc(sysdate+1) \
             and t.logicsystem='%s' \
             and t.flowstepid in ('4','E') \
             ) cnt2   from dual)" %(x,x)
        print mysql
        cur.execute(mysql)
        arr01= cur.fetchone()
        print arr01
        title='交易系统成功率'
        title = title.decode('utf-8').encode('gbk')
        db_dict = {title: arr01}
        cur.close()
        conn.close
        list[i].append(x)
        list[i].append(arr01[0])
        list[i].append(arr01[1])
        list[i].append(arr01[2])
        list[i].append(arr01[3])
        # # 遍历字典每个元素的key和value。
        # for k, v in db_dict.items():
        # # 用字典的每个key和value调用write_data_to_excel函数。
        #   write_data_to_excel(k, v)
        i=i+1
    return list
title='交易系统成功率'
title = title.decode('utf-8').encode('gbk')
arr02=get_data()
db_dict = {title: arr02}
for k, v in db_dict.items():
     write_data_to_excel(k, v)

 

你可能感兴趣的:(python,excel)