zoukankan      html  css  js  c++  java
  • python mysql到处excel

    #coding:utf8
    '''
    mysql到处数据到excel脚本

    '''
    import xlwt
    import MySQLdb
    import datetime

    conn = MySQLdb.connect(host = '192.168.96.150',port = 3306,user = 'root',passwd = 'mysql',db = 'taoyuan')
    cur = conn.cursor()
    count = cur.execute('select * from yhb')#记录条数
    print count
    print 'has %s record' %count

    #重置游标位置
    cur.scroll(0,mode='absolute')
    results = cur.fetchall()#获取所有结果

    fields = cur.description
    wbk = xlwt.Workbook()
    sheet = wbk.add_sheet('test',cell_overwrite_ok=True)#将表加入到记录簿里面

    for field in range(0,len(fields)):
    sheet.write(0,field,fields[field][0])#将表字段写入excel中

    row = 1#从第二行开始,第一行为表头
    col = 0

    for row in range(1,len(results)+1):
    for col in range(0,len(fields)):
    sheet.write(row,col,u'%s'%results[row-1][col])
    #到处路径
    out_path = 'D:/xls/'+ datetime.datetime.now().strftime('%Y%m%d') + '.xls'
    wbk.save(out_path)


  • 相关阅读:
    构建之法第九章
    九周总结学习笔记
    第一阶段对各组评价
    冲刺二阶段第八天
    冲刺二阶段第七
    冲刺二阶段第六天
    冲刺二阶段第五天
    构建之法第八章
    冲刺二阶段第四天
    冲刺二阶段第三天
  • 原文地址:https://www.cnblogs.com/tao-yuan/p/7930744.html
Copyright © 2011-2022 走看看