使用python将Sqlite中的数据直接输出为CVS
浏览:2219次 出处信息
对于SQLite来说,目前查看还是比较麻烦,所以就像把SQLite中的数据直接转成Excel中能查看的数据,这样也好在Excel中做进一步分数据处理或分析,如上篇文章中介绍的IP抓取的IP数据。从网上找到了一个将SQLite转成CVS的方法,贴在博客里,供需要的朋友使用:
import sqlite3 import csv, codecs, cStringIO class UnicodeWriter: """ A CSV writer which will write rows to CSV file "f", which is encoded in the given encoding. """
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): # Redirect output to a queue self.queue = cStringIO.StringIO() self.writer = csv.writer(self.queue, dialect=dialect, **kwds) self.stream = f self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row): self.writer.writerow([unicode(s).encode("utf-8") for s in row]) # Fetch UTF-8 output from the queue ... data = self.queue.getvalue() data = data.decode("utf-8") # ... and reencode it into the target encoding data = self.encoder.encode(data) # write to the target stream self.stream.write(data) # empty queue self.queue.truncate(0)
def writerows(self, rows): for row in rows: self.writerow(row)
conn = sqlite3.connect(\'ipaddress.sqlite3.db\') c = conn.cursor() c.execute(\'select * from ipdata\') writer = UnicodeWriter(open("export_data.csv", "wb")) writer.writerows(c)
建议继续学习:
- 在perl中连接和使用sqlite做数据存储 (阅读:4906)
- 从Mysql到Sqlite的迁移 (阅读:4057)
- SQLIte这么娇小可爱,不多了解点都不行啊 (阅读:3243)
- 关于sqlite的事务的使用 (阅读:2598)
- sqlite3导入到mysql (阅读:861)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:chrome扩展应用开发教程之开发chrome应用基础
后一篇:安全测试与渗透测试区别 >>
文章信息
- 作者:标点符 来源: 标点符
- 标签: CVS Sqlite
- 发布时间:2012-01-29 20:25:53
建议继续学习
近3天十大热文
- [596] 招聘技巧一二
- [18] 豆瓣是啥?
- [18] 密度聚类算法之OPTICS
- [16] linux内核研究笔记(一)内存管理 – p
- [16] 配合jquery实现异步加载页面元素
- [15] 我的git笔记
- [15] 在ssh服务里使用chroot
- [14] 可用性测试好助手——Morae软件的应用
- [14] 自建DNS以防止GFW干扰
- [13] 使用document.domain和ifra