• python 写excel


    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import MySQLdb
    from datetime import datetime
    import cx_Oracle
    import os
    import sys
    import xlwt
    
    reload(sys)
    import time
    sys.setdefaultencoding('utf-8')
    os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
    def write_data_to_excel(name, result):
        # 将sql作为参数传递调用get_data并将结果赋值给result,(result为一个嵌套元组)
        result = 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=['bankno', 'bankstatus', 'banktype', 'bankclscode', 'clearbank', 'legalperson', 'topbanklist', 'topbankno', 'topbankname', 'rplbankno', 'peoplebankno', 'ccpcnodeno', 'citycode', 'bankname', 'bankaliasname', 'signflag', 'address', 'postcode', 'telephone', 'email', 'remark', 'cnapsgeneration', 'saccstatus', 'saccaltdate', 'saccalttime', 'remark1', 'updatedate', 'updateno', 'effectdate', 'invaliddate', 'changetype']
        # 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中。
                #if result[i][j] is None:
                sheet.write(y, j, result[i][j])
        # 以传递的name+当前日期作为excel名称保存。
        wbk.save(name)
    
    conn = cx_Oracle.connect('afa/afa@1.1.1.1/xx')
    cur = conn.cursor()
    mysql='select distinct a.clearbank 
      from T_CPIM_CNAPSBANKINFO a 
    where a.bankno in 
           (select distinct b.legalperson from T_CPIM_CNAPSBANKINFO b)'
    
    sqlb='select count(*) from (select distinct a.clearbank  
      from T_CPIM_CNAPSBANKINFO a 
    where a.bankno in 
           (select distinct b.legalperson from T_CPIM_CNAPSBANKINFO b))'
    
    cur.execute(mysql)
    arr01= cur.fetchall()
    print type(arr01)
    
    cur.execute(sqlb)
    count=cur.fetchone()
    print count[0]
    time.sleep(5)
    list=[]
    for x in arr01:
      clearbank=x[0]
      print clearbank
      sqlb="select * from T_CPIM_CNAPSBANKINFO a where a.clearbank=%s" % (clearbank)
      print sqlb
      cur.execute(sqlb)
      arr02= cur.fetchall()
      print len(arr02)
      if len(arr02) == 1:
         list.append(arr02[0])
      else:
         minnum=1000
         for x in arr02:
           print x[13]
           min1=len(x[13])
           if min1 <  minnum:
               minnum=min1
               arr03=x
         print arr03
         print arr03[13]
         #time.sleep(100)
         list.append(arr03)
    print list
    print len(list)
    write_data_to_excel('cnaps.csv',list)
  • 相关阅读:
    Flask第31课——include标签
    flask第30篇——宏macro和import标签
    HTML第三课——css盒子
    HTML第二课——css【2】
    HTML第二课——css
    HTML第一课——基础知识普及【2】
    转一篇数据库面试题
    自创建数字证书,安装到浏览器
    【转】Base64算法详解
    【转】二维码生成原理
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348754.html
Copyright © 2020-2023  润新知