Skip to content

Commit 1504b8f

Browse files
authored
Counting_Email_in_a_Database.py
1 parent 71a6e73 commit 1504b8f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sqlite3
2+
3+
conn = sqlite3.connect('W2_Ass_2_emaildb.sqlite')
4+
cur = conn.cursor()
5+
6+
cur.execute('DROP TABLE IF EXISTS Counts')
7+
cur.execute('CREATE TABLE Counts(org TEXT, count INTEGER)')
8+
9+
fname = 'mbox.txt'
10+
fh = open(fname)
11+
12+
for line in fh:
13+
if not line.startswith('From: '):
14+
continue
15+
pieces = line.split()
16+
email = pieces[1]
17+
print("email:",email)
18+
parts = email.split('@')
19+
org = parts[-1]
20+
print("edu:",org)
21+
print()
22+
23+
cur.execute('SELECT count FROM Counts WHERE org = ?',(org,))
24+
row = cur.fetchone()
25+
26+
if row is None:
27+
cur.execute('INSERT INTO Counts (org,count) VALUES (?,1)',(org,))
28+
29+
else:
30+
cur.execute('UPDATE Counts SET count = count + 1 WHERE org = ?',(org,))
31+
conn.commit()
32+
33+
sqlstr = 'SELECT org, count FROM Counts ORDER BY count DESC LIMIT 10'
34+
35+
for row in cur.execute(sqlstr):
36+
print(str(row[0]),row[1])
37+
38+
cur.close()

0 commit comments

Comments
 (0)