Python实现将sqlite数据库导出转成Excel(xls)表的方法
作者:zouzhberk 时间:2024-01-18 02:05:10
本文实例讲述了Python实现将sqlite数据库导出转成Excel(xls)表的方法。分享给大家供大家参考,具体如下:
1. 假设已经安装带有sliqte 库的Python环境
我的是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
如果没错,会在数据库的目录下生成同名的xls文件
希望本文所述对大家Python程序设计有所帮助。
标签:Python,sqlite,Excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go语言七篇入门教程七GC垃圾回收三色标记
2024-05-05 09:28:45
![](https://img.aspxhome.com/file/2023/3/128683_0s.gif)
Echarts图表移动端横屏进入退出的实现
2024-05-11 09:06:45
![](https://img.aspxhome.com/file/2023/4/127184_0s.gif)
JS代码格式化和语法着色
2013-07-14 19:47:45
详解Vue-cli webpack移动端自动化构建rem问题
2024-04-29 13:40:06
Python 将json序列化后的字符串转换成字典(推荐)
2021-11-17 12:36:51
Golang交叉编译(跨平台编译)的使用
2024-05-02 16:26:40
![](https://img.aspxhome.com/file/2023/6/130706_0s.png)
带你轻松接触 MySQL中损坏的MyISAM表
2008-12-19 17:55:00
一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息
2023-01-09 19:50:16
![](https://img.aspxhome.com/file/2023/3/79623_0s.jpg)
MySQL 数据库存储和分支
2011-03-11 19:01:00
Go压缩位图库roaring安装使用详解
2024-05-22 10:16:42
![](https://img.aspxhome.com/file/2023/4/123904_0s.jpg)
10条改进你的CSS代码的方法
2010-03-20 22:07:00
![](https://img.aspxhome.com/file/UploadPic/20103/20/best-css-02-10s.jpg)
解决MySQL5.1安装时出现Cannot create windows service for mysql.error:0
2024-01-28 09:56:13
一文详解golang通过io包进行文件读写
2024-05-09 10:07:52
golang编程开发使用sort排序示例详解
2023-09-16 23:20:18
SQLServer 附加数据库后出现只读或失败的解决方法
2024-01-18 11:29:23
纯手工打造CSS像素画
2009-03-09 12:57:00
![](https://img.aspxhome.com/file/UploadPic/20093/9/01-12s.gif)
TypeScript 学习笔记之基本类型
2024-06-07 15:56:29
Golang 处理浮点数遇到的精度问题(使用decimal)
2024-04-25 15:01:15
![](https://img.aspxhome.com/file/2023/1/134241_0s.png)
一个超级简单的python web程序
2021-11-19 18:05:06
Python使用爬虫猜密码
2023-03-21 12:38:39
![](https://img.aspxhome.com/file/2023/6/67766_0s.png)