I'm trying to solve the assignments from the Python Coursera Course Chapter 9.4.
Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.
name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
emails = dict()
addresses = list()
for lines in handle:
line = lines.split(':')
if line[0] == 'From':
addresses.append(line[1])
for address in addresses:
emails[address] = emails.get(address, 0) + 1
bigcount = None
bigemail = None
for address,count in emails.items():
if bigcount is None or count > bigcount:
bigemail = address
bigcount = count
print(bigemail,bigcount)
That is my code so far, it runs and the output is:
cwen@iupui.edu
5
instead of the correct answer which is:
cwen@iupui.edu 5
I have tried doing lines.rstrip()
after the first for loop to get rid of spaces which then printed the two variables on the same line but added a space at the front and end, causing another mismatch from the desired output.