美文网首页我爱编程
Python Pandas 读取DB2数据,并写入Excel中

Python Pandas 读取DB2数据,并写入Excel中

作者: jianwbj | 来源:发表于2018-02-02 11:29 被阅读0次

    !/usr/bin/env python

    * coding:utf-8 *

    import pandas as pd
    import ibm_db_dbi
    import time
    import os

    dbinfo = ['1', 'DB2', '192.168.1.131', '60000', 'SAMPLE', 'db2inst', 'password']

    host = dbinfo[2]
    port = dbinfo[3]
    db = dbinfo[4]
    user = dbinfo[5]
    password = dbinfo[6]
    stmt = 'SELECT tabschema,tabname from syscat.tables'
    today = time.strftime("%Y%m%d")
    iterm = 'TEST1'
    writer = pd.ExcelWriter('./temp/{hostname}_{dbname}report{rdate}.xlsx'.format(hostname=host,dbname=db,rdate=today), engine='xlsxwriter')

    try:
    conn = (ibm_db_dbi.connect(
    "DATABASE=" + db + ";"
    "HOSTNAME=" + host + ";"
    "PORT=" + port + ";"
    "PROTOCOL=TCPIP;"
    "UID=" + user + ";"
    "PWD=" + password + ";"
    "","", ""))
    df = pd.read_sql(stmt, con=conn)

    df.to_excel(writer,sheet_name='{sheetname}'.format(sheetname=iterm),index=False)
    print df.to_json(orient='index')
    
    conn.close()
    

    except ibm_db_dbi.Error :
    print '语句执行错误:', ibm_db_dbi.Error
    conn.close()
    '''文件写入保存'''
    writer.save()

    相关文章

      网友评论

        本文标题:Python Pandas 读取DB2数据,并写入Excel中

        本文链接:https://www.haomeiwen.com/subject/naiszxtx.html