imported mpl_figure_editor from traits tutorial
[BS_AY250.git] / week5 / b.py
blob0f8eba27ae50997215b35328717d76eb246452de
1 '''
2 AY250 HW5 B
3 Build sqlite3 table of states and names
4 Author: Bryan Steinbach
5 '''
7 import os
8 import sqlite3
10 rawfn = 'candidate_names.txt'
11 tablefn = 'poll'
13 if not os.path.exists(rawfn):
14 print "Error, missing file %s"%rawfn
15 exit(1)
17 conn = sqlite3.connect('poll')
19 c = conn.cursor()
21 c.execute(''' create table names
22 (state text, gop text, dem text, ind text, incpar text)''')
24 with open(rawfn) as f:
25 f.readline() # Skip first line
26 for line in f.readlines():
27 words = line.strip().split(',')
28 state,dem,gop,ind,incpar = words
29 dem,gop,ind,incpar = [x.strip() for x in (dem,gop,ind,incpar)]
30 c.execute(''' insert into names
31 values (?,?,?,?,?) ''',(state,gop,dem,ind,incpar))
33 conn.commit()
35 # sanity check
36 c.execute('select * from names order by gop')
37 for i in range(10):
38 print c.next()
40 c.close()