Churn Email: Day of the Week
Python Project – Churn Emails – Find Which Day of the Week the Email was sent
Write a function find_email_sent_days
which reads the file /datasets/project/mbox-short.txt
and categorizes each mail message by which day of the week the email was sent.
To do this do the following:
- Open the file and read it line by line
- Look for lines that start with “From“
- For those lines which start from “From“, then look for the third word and keep a running count of each of the days of the week. How do you find the day of the week, is an exercise for you.
Note: You have to store the results in a dictionary. Only store those day of the week that exists. For Example, if there is no line for Mon then it should not be in the dictionary elements.
- At the end of the program return the contents of your
dictionary
(order does not matter)
Sample Lines from the file:
From stephen.marquard@uct.ac.za Sat Jan 5 10:14:16 2008
From stephen.marquard@uct.ac.za Sat Jan 5 15:14:16 2008
From stephen.marquard@uct.ac.za Sun Jan 6 09:14:16 2008
Output:
{'Sat': 2, 'Sun': 1}
PS – If your logic is correct then your function should return this dictionary {'Sat': 1, 'Fri': 20, 'Thu': 6}
.
Code:
def find_email_sent_days():
with open("/cxldata/datasets/project/mbox-short.txt") as f:
days = [i.split(' ')[2] for i in f if i.startswith('From ')]
print (days)
dic = {}
for day in days:
dic[day] = days.count(day)
return dic