The problem I need to solve:
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.
The sample text file is in this line: http://www.pythonlearn.com/code/mbox-short.txt
My program looks like this, but instead of returning largestvalue 5, the desired output, it returns 7, and I have no idea where that 7 comes from. The key that it returns is ok.
- Code: Select all
name = raw_input("Enter file name:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
for line in handle:
line = line.rstrip()
if line.startswith('From '):
words = line.split()
for email in words:
email = words
counts[email] = counts.get(email,0) + 1
prolemail = None
largestvalue = None
for email,count in counts.items():
if largestvalue is None or count > largestvalue:
prolemail = email
largestvalue = count