Python實現(xiàn)將SQLite中的數(shù)據(jù)直接輸出為CVS的方法示例
本文實例講述了Python實現(xiàn)將SQLite中的數(shù)據(jù)直接輸出為CVS的方法。分享給大家供大家參考,具體如下:
對于SQLite來說,目前查看還是比較麻煩,所以就像把SQLite中的數(shù)據(jù)直接轉成Excel中能查看的數(shù)據(jù),這樣也好在Excel中做進一步分數(shù)據(jù)處理或分析,如前面文章中介紹的《使用Python程序抓取新浪在國內(nèi)的所有IP》。從網(wǎng)上找到了一個將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)
更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
- Python實現(xiàn)將sqlite數(shù)據(jù)庫導出轉成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以字典形式返回查詢結果的實現(xiàn)方法
- python操作sqlite的CRUD實例分析
- 詳解Python 數(shù)據(jù)庫 (sqlite3)應用
- Python實現(xiàn)excel轉sqlite的方法
相關文章
Python使用wxPython和PyMuPDF實現(xiàn)合并PDF文檔
處理大量的PDF文檔可能會變得復雜和耗時,但是,使用Python編程和一些強大的庫,可以使這個任務變得簡單而高效,下面我們就來看看Python如何使用wxPython和PyMuPDF合并PDF文檔并自動復制到剪貼板吧2023-11-11基于tkinter中ttk控件的width-height設置方式
這篇文章主要介紹了基于tkinter中ttk控件的width-height設置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05python 使用GDAL實現(xiàn)柵格tif轉矢量shp的方式小結
今天通過本文給大家分享python 使用GDAL實現(xiàn)柵格tif轉矢量shp的方式小結,計劃是使用柵格轉矢量的方式,將柵格數(shù)據(jù)轉為矢量shp文件,然后進行矢量切片,使用Mapbox進行前端動態(tài)渲染,具體內(nèi)容詳情跟隨小編一起看看吧2021-08-08