-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_15.1.py
More file actions
33 lines (26 loc) · 837 Bytes
/
chapter_15.1.py
File metadata and controls
33 lines (26 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import sqlite3
conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS Counts')
cur.execute('''
CREATE TABLE Counts (org TEXT, count INTEGER)''')
fname = 'mbox.txt'
fh = open(fname)
for line in fh:
if not line.startswith ('From:'): continue
pieces = line.split()
email = pieces[1]
dom = email.split('@')
dname = dom[1]
cur.execute('SELECT count FROM Counts WHERE org = ? ',(dname,))
row = cur.fetchone()
if row is None:
cur.execute(''' INSERT INTO Counts (org,count)
VALUES (?,1)''',(dname,))
else:
cur.execute('UPDATE Counts SET count = count + 1 WHERE org = ?', (dname,))
conn.commit()
sqlstr = 'SELECT org, count FROM Counts ORDER BY count'
for row in cur.execute(sqlstr):
print(str(row[0]), row[1])
cur.close()