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 |
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)' ) filename = input ( "Enter file name: " ) if len (filename) < 1 : filename = 'new.txt' fh = open (filename, "r" ) g = fh.read() for line in g: 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() sqlstr = 'SELECT email, count FROM Counts ORDER BY count DESC LIMIT 10' for row in cur.execute(sqlstr): print (row[ 0 ], row[ 1 ]) cur.close() |
Larz60+ write Feb-22-2023, 08:57 PM:
Please do not use attachments, instead post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
I have done this for you this time, please use BBCode on future posts
Please do not use attachments, instead post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
I have done this for you this time, please use BBCode on future posts