imported mpl_figure_editor from traits tutorial
[BS_AY250.git] / week5 / c.py
blob6d3da491f4ac4d5f12468a374f53c4a758803c6b
1 '''
2 AY250 HW5 C
3 Build sqlite3 table of names and pictures
4 Author: Bryan Steinbach
5 '''
7 import os
8 import sqlite3
10 rawfn = 'candidate_names.txt'
11 tablefn = 'poll'
13 base_url = 'http://astro.berkeley.edu/~amorgan/candidates/'
14 ext = '.gif'
16 if not os.path.exists(rawfn):
17 print "Error, missing file %s"%rawfn
18 exit(1)
20 conn = sqlite3.connect('poll')
22 c = conn.cursor()
24 c.execute(''' create table pics
25 (name text, url text) ''')
27 with open(rawfn) as f:
28 f.readline() # Skip first line
29 for line in f.readlines():
30 words = line.strip().split(',')
31 state,dem,gop,ind,incpar = words
32 names = [x.strip() for x in (dem,gop,ind) if x.strip() != '']
34 for name in names:
35 c.execute(''' insert into pics
36 values (?,?) ''',(name,base_url+name+ext))
38 conn.commit()
40 # sanity check
41 c.execute('select * from pics order by name')
42 for i in range(10):
43 print c.next()
45 c.close()