国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2019-11-25 16:00:53
字體:
供稿:網(wǎng)友

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

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

我的是Python2.5

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

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

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

import sqlite3 as sqlitefrom 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

如果沒錯,會在數(shù)據(jù)庫的目錄下生成同名的xls文件

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 博野县| 峨边| 交城县| 临城县| 金沙县| 疏附县| 丹棱县| 育儿| 黔江区| 凌源市| 海阳市| 呼图壁县| 原平市| 清丰县| 旌德县| 长顺县| 晋州市| 绩溪县| 璧山县| 固镇县| 北流市| 临邑县| 台江县| 宜都市| 葵青区| 留坝县| 郴州市| 广元市| 丹江口市| 金阳县| 延边| 唐河县| 英超| 祁东县| 南岸区| 郸城县| 海安县| 肇东市| 万安县| 娱乐| 西安市|