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
|
import sqlite3
connection = sqlite3.connect('domainDB.sqlite')
cursor = connection.cursor()
cursor.execute('drop table if exists Counts')
cursor.execute('create table Counts (domain text, count integer)')
fileHandler = open('mbox.txt')
counts = dict() domainList = list()
for line in fileHandler: if not line.startswith('From: '): continue pieces = line.split() email = pieces[1] pieces2 = email.split('@') domain = pieces2[1] domainList.append(domain)
for domain in domainList: counts[domain] = counts.get(domain, 0) + 1 print(counts)
for domain, count in counts.items(): cursor.execute('insert into Counts (domain, count) values (?, ?)', (domain, count))
connection.commit()
sqlstr = 'select domain, count from Counts order by count desc' for row in cursor.execute(sqlstr): print(str(row[0]), row[1])
connection.close()
|
近期评论