I need to search for the hours of the day that each email was sent, then I need to create a dictionary from those. Then I need to print the dictionary at the end with the count of how many times an email was sent at each hour.

Here is a sample of how it should look:
Sample Execution:
python timeofday.py
Enter a file name: mbox-short.txt
04 3
06 1
07 1
09 2
10 3
11 6
14 1

So basically I am stuck on how to search for the hours and add them to the dictionary. Can anyone help me please? Thank you very much!

import string

fhandler = open('mbox-short.txt')

counts = dict()
for line in fhandler:
    emails = line.split('@')
    for email in emails:
        counts[email] = counts.get(email, 0) + 1
        
lst = list()
for key, val in counts.items():
    lst.append( (val, key) )
lst.sort(reverse=True)

for val, key in lst[:1] :
    print key, val

Recommended Answers

All 2 Replies

The only things you can search for that I can see would be two colons in the line, str.count(), and if found, "Jan" through "Dec" (or "Mon" through "Sun" but they don't all contain the day). If they are both found, you can split and search for the string that has the colon in it, which should be the time.

The only things you can search for that I can see would be two colons in the line, str.count(), and if found, "Jan" through "Dec" (or "Mon" through "Sun" but they don't all contain the day). If they are both found, you can split and search for the string that has the colon in it, which should be the time.

Thank you, this advice helped. I did need to search for the colon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.