国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實(shí)現(xiàn)將SQLite中的數(shù)據(jù)直接輸出為CVS的方法示例

2020-01-04 16:59:42
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python實(shí)現(xiàn)將SQLite中的數(shù)據(jù)直接輸出為CVS的方法。分享給大家供大家參考,具體如下:

對(duì)于SQLite來說,目前查看還是比較麻煩,所以就像把SQLite中的數(shù)據(jù)直接轉(zhuǎn)成Excel中能查看的數(shù)據(jù),這樣也好在Excel中做進(jìn)一步分?jǐn)?shù)據(jù)處理或分析,如前面文章中介紹的《使用Python程序抓取新浪在國(guó)內(nèi)的所有IP》。從網(wǎng)上找到了一個(gè)將SQLite轉(zhuǎn)成CVS的方法,貼在這里,供需要的朋友使用:

import sqlite3import csv, codecs, cStringIOclass 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)

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊宁市| 浮山县| 肃北| 贡嘎县| 江川县| 祁门县| 呼和浩特市| 田东县| 虹口区| 仙桃市| 玛纳斯县| 万载县| 阿克陶县| 开鲁县| 广昌县| 高雄市| 滨海县| 东兴市| 宽甸| 赣州市| 铁力市| 寿阳县| 离岛区| 漳浦县| 曲麻莱县| 灵川县| 光泽县| 略阳县| 盐池县| 拜城县| 武胜县| 东丽区| 宁河县| 墨竹工卡县| 图们市| 大竹县| 安化县| 土默特左旗| 河北区| 桦南县| 惠东县|