3 Build sqlite3 table of polls
4 Author: Bryan Steinbach
10 rawfn
= 'senate_polls.raw'
13 if not os
.path
.exists(rawfn
):
14 print "Error, missing file %s"%rawfn
17 if os
.path
.exists(tablefn
):
20 conn
= sqlite3
.connect('poll')
24 months
= ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
26 c
.execute(''' create table polls
27 (state text, gop int, dem int, ind int, date1 text, date2 text, pollster text)''')
29 def format_date(month
,day
):
30 moni
= months
.index(month
)+1
35 return '2010-'+mons
+'-'+day
37 with
open(rawfn
) as f
:
38 for line
in f
.readlines():
39 words
= line
.strip().split()
40 state
,gop
,dem
,ind
,mon1
,day1
,mon2
,day2
= words
[:8]
42 date1
= format_date(mon1
,day1
)
43 date2
= format_date(mon2
,day2
)
45 pollster
= ' '.join(words
[8:])
46 c
.execute(''' insert into polls
47 values (?,?,?,?,?,?,?) ''',(state
,gop
,dem
,ind
,date1
,date2
,pollster
))
52 c
.execute('select * from polls order by date1 desc')