最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)將sqlite數(shù)據(jù)庫(kù)導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法

 更新時(shí)間:2017年07月17日 09:49:21   作者:zouzhberk  
這篇文章主要介紹了Python實(shí)現(xiàn)將sqlite數(shù)據(jù)庫(kù)導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法,結(jié)合實(shí)例形式分析了Python針對(duì)sqlite數(shù)據(jù)庫(kù)的連接、讀取及使用寫操作包(xlwt)生成Excel表的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)將sqlite數(shù)據(jù)庫(kù)導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法。分享給大家供大家參考,具體如下:

1. 假設(shè)已經(jīng)安裝帶有sliqte 庫(kù)的Python環(huán)境

我的是Python2.5

2. 下載 python xls 寫操作包(xlwt)并安裝

下載地址: http://pypi.python.org/pypi/xlwt

3. 下面就是代碼(db2xls.py):

import sqlite3 as sqlite
from xlwt import *
#MASTER_COLS = ['rowid', 'type','name','tbl_name', 'rootpage','sql']
def sqlite_get_col_names(cur, table):
  query = 'select * from %s' % table
  cur.execute(query)
  return [tuple[0] for tuple in cur.description]
def sqlite_query(cur, table, col = '*', where = ''):
  if where != '':
    query = 'select %s from %s where %s' % (col, table, where)
  else:
    query = 'select %s from %s ' % (col, table)
  cur.execute(query)
  return cur.fetchall()
def sqlite_to_workbook(cur, table, workbook):
  ws = workbook.add_sheet(table)
  print 'create table %s.' % table
  for colx, heading in enumerate(sqlite_get_col_names(cur, table)):
      ws.write(0,colx, heading)
  for rowy,row in enumerate(sqlite_query(cur, table)):
    for colx, text in enumerate(row):
      ws.write(rowy+ 1, colx, text)
def main(dbpath):
  xlspath = dbpath[0:dbpath.rfind('.')] + '.xls'
  print "<%s> --> <%s>"% (dbpath, xlspath)
  db = sqlite.connect(dbpath)
  cur = db.cursor()
  w = Workbook()
  for tbl_name in [row[0] for row in sqlite_query(cur, 'sqlite_master', 'tbl_name', 'type = \'table\'')]:
    sqlite_to_workbook(cur,tbl_name, w)
  cur.close()
  db.close()
  if tbl_name !=[]: w.save(xlspath)
if __name__ == "__main__":
  # arg == database path
  main(sys.argv[1])

4. 用法:

> python  <path>/db2xls.py  dbpath

如果沒(méi)錯(cuò),會(huì)在數(shù)據(jù)庫(kù)的目錄下生成同名的xls文件

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

崇信县| 邵阳市| 蒙自县| 洛扎县| 闻喜县| 开远市| 龙川县| 望奎县| 沭阳县| 岳池县| 广元市| 黔西| 永春县| 湖口县| 南乐县| 崇义县| 长岛县| 平度市| 淳安县| 赞皇县| 分宜县| 涪陵区| 灵宝市| 辽宁省| 江安县| 绿春县| 镇沅| 丽江市| 临朐县| 阳山县| 阆中市| 抚顺县| 南宁市| 翁源县| 固安县| 镇巴县| 渝中区| 南江县| 兴宁市| 太保市| 元朗区|