Created
June 29, 2012 05:23
-
-
Save moluapple/3015959 to your computer and use it in GitHub Desktop.
将 sql 数据导出为 csv 文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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