Python实现将SQLite中的数据直接输出为CVS的方法示例


Posted in Python onJuly 13, 2017

本文实例讲述了Python实现将SQLite中的数据直接输出为CVS的方法。分享给大家供大家参考,具体如下:

对于SQLite来说,目前查看还是比较麻烦,所以就像把SQLite中的数据直接转成Excel中能查看的数据,这样也好在Excel中做进一步分数据处理或分析,如前面文章中介绍的《使用Python程序抓取新浪在国内的所有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)

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python实现栈的方法
May 26 Python
Python的“二维”字典 (two-dimension dictionary)定义与实现方法
Apr 27 Python
tensorflow输出权重值和偏差的方法
Feb 10 Python
详解django三种文件下载方式
Apr 06 Python
对Python 窗体(tkinter)树状数据(Treeview)详解
Oct 11 Python
python实现AES加密解密
Mar 28 Python
Python模块汇总(常用第三方库)
Oct 07 Python
python如何基于redis实现ip代理池
Jan 17 Python
Python语法之精妙的十个知识点(装B语法)
Jan 18 Python
python如何使用代码运行助手
Jul 03 Python
Django框架实现在线考试系统的示例代码
Nov 30 Python
matplotlib制作雷达图报错ValueError的实现
Jan 05 Python
简单易懂的python环境安装教程
Jul 13 #Python
Python2.7读取PDF文件的方法示例
Jul 13 #Python
Python使用win32com实现的模拟浏览器功能示例
Jul 13 #Python
python3 模拟登录v2ex实例讲解
Jul 13 #Python
python计算auc指标实例
Jul 13 #Python
Python实现MySQL操作的方法小结【安装,连接,增删改查等】
Jul 12 #Python
Python实现统计代码行的方法分析
Jul 12 #Python
You might like
PHP Memcached + APC + 文件缓存封装实现代码
2010/03/11 PHP
PHP无法访问远程mysql的问题分析及解决
2013/05/16 PHP
基于PHP array数组的教程详解
2013/06/05 PHP
PHP中使用Imagick操作PSD文件实例
2015/01/26 PHP
PHP从FLV文件获取视频预览图的方法
2015/03/12 PHP
thinkPHP框架对接支付宝即时到账接口回调操作示例
2016/11/14 PHP
Laravel源码解析之路由的使用和示例详解
2018/09/27 PHP
Jquery选择器中使用变量实现动态选择例子
2014/07/25 Javascript
JavaScript实现带箭头标识的多级下拉菜单效果
2015/08/27 Javascript
js获取iframe中的window对象的实现方法
2016/05/20 Javascript
Angularjs的键盘事件的绑定
2017/07/27 Javascript
在vue中实现简单页面逆传值的方法
2017/11/27 Javascript
如何在基于vue-cli的项目自定义打包环境
2018/11/10 Javascript
vue项目首屏加载时间优化实战
2019/04/23 Javascript
React Hooks 实现和由来以及解决的问题详解
2020/01/17 Javascript
jQuery实现视频展示效果
2020/05/30 jQuery
[10:49]2014国际邀请赛 叨叨刀塔第二期为真正的电竞喝彩
2014/07/21 DOTA
python zip文件 压缩
2008/12/24 Python
python实现登陆知乎获得个人收藏并保存为word文件
2015/03/16 Python
pycharm安装图文教程
2017/05/02 Python
Java及python正则表达式详解
2017/12/27 Python
python实现转盘效果 python实现轮盘抽奖游戏
2019/01/22 Python
python实现年会抽奖程序
2019/01/22 Python
用python 实现在不确定行数情况下多行输入方法
2019/01/28 Python
django处理select下拉表单实例(从model到前端到post到form)
2020/03/13 Python
使用Python构造hive insert语句说明
2020/06/06 Python
localStorage 设置过期时间的方法实现
2018/12/21 HTML / CSS
物业经理求职自我评价
2013/09/22 职场文书
初一体育教学反思
2014/01/29 职场文书
百货商场楼层班组长竞聘书
2014/03/31 职场文书
《黄山奇石》教学反思
2014/04/19 职场文书
感恩父母的演讲稿
2014/05/06 职场文书
中国梦团日活动总结
2014/07/07 职场文书
2014年效能监察工作总结
2014/11/21 职场文书
高中物理教学反思
2016/02/19 职场文书
MySQL Router实现MySQL的读写分离的方法
2021/05/27 MySQL