In [16]:
import sqlite3
conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS Counts')
cur.execute('''
CREATE TABLE Counts (email TEXT, count INTEGER)''')
fname = input('Enter file name: ')
if (len(fname) < 1): fname = 'mbox.txt'
fh = open(fname)
for line in fh:
if not line.startswith('From: '): continue
pieces = line.split()
email = pieces[1]
cur.execute('SELECT count FROM Counts WHERE email = ? ', (email,))
row = cur.fetchone()
if row is None:
cur.execute('''INSERT INTO Counts (email, count)
VALUES (?, 1)''', (email,))
else:
cur.execute('UPDATE Counts SET count = count + 1 WHERE email = ?',
(email,))
conn.commit()
# https://www.sqlite.org/lang_select.html
sqlstr = 'SELECT email, count FROM Counts ORDER BY count DESC LIMIT 10'
for row in cur.execute(sqlstr):
print(str(row[0]), row[1])
cur.close()
[email protected] 195 [email protected] 161 [email protected] 158 [email protected] 111 [email protected] 110 [email protected] 96 [email protected] 93 [email protected] 90 [email protected] 84 [email protected] 67
In [24]:
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'
if (len(fname) < 1) :
fname = 'mbox-short.txt'
fh = open(fname)
for line in fh:
if not line.startswith('From: ') :
continue
pieces = line.split()
email = pieces[1]
parts = email.split('@')
org = parts[1]
cur.execute('SELECT count FROM Counts WHERE org = ? ', (org,))
row = cur.fetchone()
if row is None:
cur.execute('INSERT INTO Counts (org, count) VALUES (?, 1)', (org,))
else:
cur.execute('UPDATE Counts SET count = count + 1 WHERE org = ?', (org,))
conn.commit()
sqlstr = 'SELECT org, count FROM Counts ORDER BY count DESC LIMIT 10'
for row in cur.execute(sqlstr):
print(str(row[0]), row[1])
cur.close()
iupui.edu 536 umich.edu 491 indiana.edu 178 caret.cam.ac.uk 157 vt.edu 110 uct.ac.za 96 media.berkeley.edu 56 ufp.pt 28 gmail.com 25 et.gatech.edu 17
In [ ]: