Reputation: 11
So i'm learning programming through freecodecamp and I am stuck on one of the exercises offered by the professor teaching the course. Here is the exercise in question:
Exercise 2: Write a program that categorizes each mail message by which day of the week the commit was done. To do this look for lines that start with "From", then look for the third word and keep a running count of each of the days of the week. At the end of the program print out the contents of your dictionary (order does not matter).
Here is the email log in question http://www.py4e.com/code3/mbox.txt
Here is as far as I have successfully gotten with writing my own code for this problem:
filein = input('Input an email log txt file please: ')
emaillog = open(filein)
days = dict()
for x in emaillog:
if 'From ' in x:
print(x.split())
else:
continue
I've noticed that it prints out a multitude of list all split nicely with the area I care about being at index [2] of all of them. However, I don't understand how I'm supposed to treat the data to extrapolate the days and build my dictionary.
EDIT: Thank you for the help guys, here is what I ended up coming up with that gave me the right results:
filein = input('Input an email log txt file please: ')
emaillog = open(filein)
days = dict()
lis = list()
for x in emaillog:
if 'From ' in x:
array = x.split()
day = array[2]
days[day] = days.get(day, 0)+1
else:
continue
print(days)
Upvotes: 1
Views: 136
Reputation: 46
Here is how you can do this task with the idea of the algorithm you made
filein = input('Input an email log txt file please: ')
emaillog = open(filein)
days = dict()
for x in emaillog:
if 'From ' in x[:5]:
L = x.split()
if L[2] in days:
days[L[2]] +=1
else:
days[L[2]] =0
output:
{'Sat': 60, 'Fri': 314, 'Thu': 391, 'Wed': 291, 'Tue': 371, 'Mon': 298, 'Sun': 65}
But you can use RegEx here.
Upvotes: 1
Reputation: 123423
I'm not going to do the whole exercise for you, but the following ought give you a running start:
filein = input('Input an email log txt file please: ')
days = dict()
with open(filein) as emaillog:
for line in emaillog:
if line.startswith('From'):
print(line, end='')
row = line.split() # Convert line into list of words.
# Get day from list and add 1 to its counter in the days dict.
...
print(days)
Upvotes: 0