From b7c56e15de3bb3d798ec6ea70c765ae286f41aa2 Mon Sep 17 00:00:00 2001 From: Bryan Steinbach Date: Wed, 6 Oct 2010 13:25:27 -0700 Subject: [PATCH] c complete --- week5/c.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 week5/c.py diff --git a/week5/c.py b/week5/c.py new file mode 100644 index 0000000..9c5fce1 --- /dev/null +++ b/week5/c.py @@ -0,0 +1,48 @@ +''' +AY250 HW5 C +Build sqlite3 table of names and pictures +Author: Bryan Steinbach +''' + +import os +import sqlite3 + +rawfn = 'candidate_names.txt' +tablefn = 'poll' + +base_url = 'http://astro.berkeley.edu/~amorgan/candidates/' +ext = '.gif' + +if not os.path.exists(rawfn): + print "Error, missing file %s"%rawfn + exit(1) + +if os.path.exists(tablefn): + os.remove(tablefn) + +conn = sqlite3.connect('poll') + +c = conn.cursor() + +c.execute(''' create table pics + (name text, url text) ''') + +with open(rawfn) as f: + f.readline() # Skip first line + for line in f.readlines(): + words = line.split(',') + state,dem,gop,ind,incpar = words + names = [x.strip() for x in (dem,gop,ind) if x.strip() != ''] + + for name in names: + c.execute(''' insert into pics + values (?,?) ''',(name,base_url+name+ext)) + +conn.commit() + +# sanity check +c.execute('select * from pics order by name') +for i in range(10): + print c.next() + +c.close() -- 2.11.4.GIT