Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created June 29, 2012 05:23
Show Gist options
  • Save moluapple/3015959 to your computer and use it in GitHub Desktop.
Save moluapple/3015959 to your computer and use it in GitHub Desktop.
将 sql 数据导出为 csv 文件
# -*- coding: utf-8 -*-
# http://stackoverflow.com/questions/3710263/how-do-i-create-a-csv-file-from-database-in-python
# 如遇编码错误,可使用python3
import csv
import sqlite3
conn = sqlite3.connect("pcd.db")
cursor = conn.cursor()
cursor.execute("select * from domain_data;")
csv_writer = csv.writer(open("out.csv", "wt"))
csv_writer.writerow([i[0] for i in cursor.description]) # write headers
csv_writer.writerows(cursor)
del csv_writer # this will close the CSV file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment