Python實現(xiàn)將SQLite中的數(shù)據(jù)直接輸出為CVS的方法示例
本文實例講述了Python實現(xiàn)將SQLite中的數(shù)據(jù)直接輸出為CVS的方法。分享給大家供大家參考,具體如下:
對于SQLite來說,目前查看還是比較麻煩,所以就像把SQLite中的數(shù)據(jù)直接轉(zhuǎn)成Excel中能查看的數(shù)據(jù),這樣也好在Excel中做進一步分數(shù)據(jù)處理或分析,如前面文章中介紹的《使用Python程序抓取新浪在國內(nèi)的所有IP》。從網(wǎng)上找到了一個將SQLite轉(zhuǎn)成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)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- Python實現(xiàn)將sqlite數(shù)據(jù)庫導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法
- Python解析excel文件存入sqlite數(shù)據(jù)庫的方法
- Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實例
- Python標準庫之sqlite3使用實例
- python操作數(shù)據(jù)庫之sqlite3打開數(shù)據(jù)庫、刪除、修改示例
- 在Python中使用SQLite的簡單教程
- python實現(xiàn)在sqlite動態(tài)創(chuàng)建表的方法
- Python Sqlite3以字典形式返回查詢結(jié)果的實現(xiàn)方法
- python操作sqlite的CRUD實例分析
- 詳解Python 數(shù)據(jù)庫 (sqlite3)應(yīng)用
- Python實現(xiàn)excel轉(zhuǎn)sqlite的方法
相關(guān)文章
python字典與json轉(zhuǎn)換的方法總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python字典與json轉(zhuǎn)換的方法總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-12-12
Python?flask框架post接口調(diào)用示例
這篇文章主要介紹了Python?flask框架post接口調(diào)用,結(jié)合實例形式分析了基于flask框架的post、get請求響應(yīng)及接口調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
基于Python創(chuàng)建語音識別控制系統(tǒng)
這篇文章主要介紹了通過Python實現(xiàn)創(chuàng)建語音識別控制系統(tǒng),能利用語音識別識別說出來的文字,根據(jù)文字的內(nèi)容來控制圖形移動,感興趣的同學(xué)可以關(guān)注一下2021-12-12

