-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcsv2sqlite.py
More file actions
157 lines (143 loc) · 5.47 KB
/
csv2sqlite.py
File metadata and controls
157 lines (143 loc) · 5.47 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# libraries for loading a CSV file into a sqlite database
import atexit
import csv
import os
import os.path
import re
import sqlite3
import string
import sys
import tempfile
class ColType:
INT = 0
REAL = 1
TEXT = 2
strMap = { INT: 'integer', REAL: 'real', TEXT: 'text' }
# compiled regex to match a float or int:
# Also allows commas and leading $
intRE = re.compile('[-+]?[$]?[0-9,]+')
realRE = re.compile('[-+]?[$]?[0-9,]*\.?[0-9]+([eE][-+]?[0-9]+)?')
def guessColumnType( cg, cs ):
"""Given the current guess (or None) for a column type and cell value string cs
make a conservative guess at column type.
We use the order int <: real <: text, and a guess will only become more general.
TODO: support various date formats....
"""
if cg==ColType.TEXT:
return ColType.TEXT
if len( cs ) == 0:
return cg # empty cells don't affect current guess
if cg==None or cg==ColType.INT:
match = intRE.match( cs )
if match and match.start()==0 and match.end()==len( cs ):
return ColType.INT
if cg!=ColType.TEXT:
match = realRE.match( cs )
if match and match.start()==0 and match.end()==len( cs ):
return ColType.REAL
return ColType.TEXT
def genColumnIds( colHeaders ):
"""Use descriptive strings from first (header) row of CSV to generate column identifiers for database.
Tries to use the first word of each description to generate a human-friendly column name, but falls back
to simpler 'col'N if that fails.
Will fail in an unlikely edge case.
"""
# try to generate a reasonable field name from the given columnName:
colNames = []
colNameMap = {}
for i, cdesc in enumerate(colHeaders):
words = re.findall( '\w+', cdesc )
if len(words)==0 or words[0] in colNameMap:
cid = "col" + str(i)
else:
cid = words[0]
colNames.append( ( cid, cdesc ) )
colNameMap[ cid ] = True
return colNames
def parseType( ct, vs):
"""parse string vs to a Python value based on SQL type named ct
"""
if len(vs)==0 and ct!="text":
return None
if ct=="integer":
# for now: drop all $ and , chars:
cs = vs.strip().translate( None, "$," )
ret = int( cs )
elif ct=="real":
# for now: drop all $ and , chars:
cs = vs.strip().translate( None, "$," )
ret = float( cs )
else:
ret = vs
return ret
def createColumnTable( dbConn, tableName, colIdInfo ):
"""Create a metadata table to retain descriptive column names
"""
schemaStr = "('id' integer, 'colName' text, 'description' text)"
colTableName = tableName + "_columnInfo"
dropStr = "DROP TABLE IF EXISTS " + colTableName
dbConn.execute( dropStr )
createStr = "CREATE TABLE " + colTableName + " " + schemaStr
dbConn.execute( createStr )
insertStmt = "INSERT INTO " + colTableName + " VALUES ( ?, ?, ? )"
for id,(colName,colDesc) in enumerate( colIdInfo ):
rowVals = [ id, colName, colDesc ]
dbConn.execute( insertStmt, rowVals )
def guessColumnTypes( csvfile, rd, nCols ):
"""Given a CSV file positioned after header, take a full pass and make a
conservative guess at column types."""
colTypes = [ None ] * nCols
for row in rd:
colTypes = map( guessColumnType, colTypes, row )
# now lift the column types:
def liftCT( ct ):
if ct==None:
return ColType.TEXT
return ct
colTypes = map( liftCT, colTypes )
colTypes = map( lambda ct: ColType.strMap[ ct ], colTypes )
return colTypes
def loadCSVFile( dbName, csvFilePath, **fmtparams ):
"""Attempt to open and parse the specified CSV file and load it in to an in-memory sqlite table.
Returns: name of sqllite table
"""
bnm = os.path.basename( csvFilePath )
(tableName,_)= os.path.splitext( bnm )
dbConn = sqlite3.connect(dbName)
with open(csvFilePath) as csvfile:
rd = csv.reader( csvfile, **fmtparams )
headerRow = rd.next()
colIdInfo = genColumnIds( headerRow )
createColumnTable( dbConn, tableName, colIdInfo )
colNames = map( lambda (cn, _) : cn, colIdInfo )
colTypes = guessColumnTypes( csvfile, rd, len( colIdInfo ) )
# now rewind to beginning of file:
csvfile.seek( 0 )
rd = csv.reader( csvfile, **fmtparams )
rd.next() # skip header
# build up Schema string:
typedCols = map( lambda cn, ct: "'" + cn + "' " + ct, colNames, colTypes )
schemaStr = string.join( typedCols, ", " )
dropStr = "DROP TABLE IF EXISTS " + tableName
dbConn.execute( dropStr )
createStr = "CREATE TABLE " + tableName + " ( " + schemaStr + " )"
# print createStr
dbConn.execute( createStr )
qs = ['?'] * len(colNames)
insertStmt = "INSERT INTO " + tableName + " VALUES ( " + string.join( qs, ", " ) + " ) "
for (i,row) in enumerate( rd ):
rowVals = map( parseType, colTypes, row )
# if i < 10:
# print rowVals
dbConn.execute( insertStmt, rowVals )
dbConn.commit()
return tableName
def createTempDb():
"""Create a sqlite database backed by a file in /tmp. Attempts to remove db on exit."""
tf = tempfile.NamedTemporaryFile(prefix='csvview',suffix='.db',dir='/tmp')
dbName = tf.name
atexit.register( lambda f: f.close(), tf )
return dbName
if __name__ == "main":
dbName = createTempDb()
tableName = loadCSVFile( dbName, sys.argv[1] )