c complete
[BS_AY250.git] / week5 / c.py
blob9c5fce170f90435e03aebeb71637d4342555a621
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 if os.path.exists(tablefn):
21 os.remove(tablefn)
23 conn = sqlite3.connect('poll')
25 c = conn.cursor()
27 c.execute(''' create table pics
28 (name text, url text) ''')
30 with open(rawfn) as f:
31 f.readline() # Skip first line
32 for line in f.readlines():
33 words = line.split(',')
34 state,dem,gop,ind,incpar = words
35 names = [x.strip() for x in (dem,gop,ind) if x.strip() != '']
37 for name in names:
38 c.execute(''' insert into pics
39 values (?,?) ''',(name,base_url+name+ext))
41 conn.commit()
43 # sanity check
44 c.execute('select * from pics order by name')
45 for i in range(10):
46 print c.next()
48 c.close()