-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertSQLiteToCSV.py
executable file
·57 lines (48 loc) · 1.38 KB
/
convertSQLiteToCSV.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
from BeautifulSoup import BeautifulStoneSoup
import codecs
import cgi
import sqlite3
import string
import sys
#Converts HTML entities to unicode. For example '&' becomes '&'
def HTMLEntitiesToUnicode(text):
text = unicode(BeautifulStoneSoup(text, convertEntities=BeautifulStoneSoup.ALL_ENTITIES))
return text
if __name__ == "__main__":
try:
conn = sqlite3.connect('MailingListData.db')
c = conn.cursor()
except sqlite3.Error, e:
print "Error occurred:", e.args[0]
sys.exit(1)
try:
c.execute("SELECT * FROM MailingListData")
except sqlite3.Error, e:
print "Error occurred:", e.args[0]
sys.exit(1)
f = file("MailingListData.csv", "w")
f = codecs.open("MailingListData.csv", "w", "utf-8-sig")
col_name_list = [tuple[0] for tuple in c.description]
for col_name in col_name_list:
f.write(col_name)
if col_name == col_name_list[-1]:
f.write("\n")
else:
f.write(", ")
for row in c:
for field in row:
if type(field) is unicode:
noCommas = string.replace(field,","," ")
#uni = HTMLEntitiesToUnicode(noCommas).encode('utf-8')
uni = HTMLEntitiesToUnicode(noCommas)
#f.write(uni.encode('utf-8'))
f.write(uni)
else:
f.write(unicode(field))
if field == row[-1]:
f.write("\n")
else:
f.write(", ")
f.close()
c.close()