Python實(shí)現(xiàn)將sqlite數(shù)據(jù)庫(kù)導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法
本文實(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
如果沒錯(cuò),會(huì)在數(shù)據(jù)庫(kù)的目錄下生成同名的xls文件
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(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)文章
Python Tkinter Entry和Text的添加與使用詳解
這篇文章主要介紹了Python Tkinter Entry和Text的添加與使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python中的pyecharts庫(kù)使用總結(jié)
這篇文章主要介紹了Python中的pyecharts庫(kù)使用總結(jié),Pyecharts 提供了一個(gè)簡(jiǎn)單而直觀的 API 接口,使得使用者無(wú)需了解復(fù)雜的 JavaScript 語(yǔ)法,即可通過 Python 代碼實(shí)現(xiàn)高度定制化的圖表設(shè)計(jì),需要的朋友可以參考下2023-12-12
python實(shí)現(xiàn)簡(jiǎn)單tftp(基于udp協(xié)議)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單tftp,基于udp協(xié)議,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
在 Python 應(yīng)用中使用 MongoDB的方法
這篇文章主要介紹了在 Python 應(yīng)用中使用 MongoDB的方法,需要的朋友可以參考下2017-01-01
Python的Django框架中使用SQLAlchemy操作數(shù)據(jù)庫(kù)的教程
SQLAlchemy是Python一個(gè)專門的數(shù)據(jù)庫(kù)管理工具,如果對(duì)Django ORM覺得有些生疏的話完全可以結(jié)合SQLAlchemy,這里我們就來總結(jié)一下Python的Django框架中使用SQLAlchemy操作數(shù)據(jù)庫(kù)的教程2016-06-06
python自動(dòng)發(fā)郵件庫(kù)yagmail的示例代碼
本篇文章主要介紹了python自動(dòng)發(fā)郵件庫(kù)yagmail的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
python實(shí)現(xiàn)n個(gè)數(shù)中選出m個(gè)數(shù)的方法
今天小編就為大家分享一篇python實(shí)現(xiàn)n個(gè)數(shù)中選出m個(gè)數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11

