-
Notifications
You must be signed in to change notification settings - Fork 4
/
Counting_Organizations
52 lines (38 loc) · 1.27 KB
/
Counting_Organizations
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# This application will read the mailbox data (mbox.txt) count up the number email messages per organization
# (i.e. domain name of the email address) using a database with the following schema to maintain the counts.
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 = raw_input('Enter file name: ')
while(len(fname) < 1):
print 'Please enter file name again!'
fh = open(fname)
corporation = list()
for line in fh:
if line.startswith('From: '):
pieces = line.split()
email = pieces[1]
index = email.find('@')
corporation.append(email[index+1:len(email)])
corporation = list(set(corporation))
number = dict.fromkeys(corporation, 0)
fh = open(fname)
for line in fh:
if line.startswith('From: '):
pieces = line.split()
email = pieces[1]
index = email.find('@')
for item in number.keys():
if email[index+1:len(email)] == item:
number[item] = number[item] + 1
for item in number.keys():
cur.execute('''INSERT INTO Counts(org, count) VALUES(?, ?)''', [item,number[item]])
conn.commit()
sqlstr = 'SELECT org, count FROM Counts ORDER BY count DESC'
for row in cur.execute(sqlstr):
print str(row[0]),row[1]
cur.close()