Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3.2
- # -*- coding: utf-8 -*-
- # main.py
- """ Task: Exercise 10.1
- Revise a previous program as follows: Read and parse the βFromβ lines and
- pull out the addresses from the line. Count the number of messages from
- each person using a dictionary.
- After all the data has been read print the person with the most commits by
- creating a list of (count, email) tuples from the dictionary and then
- sorting the list in reverse order and print out the person who has the most
- commits.
- Sample Line:
- From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
- Enter a file name: mbox-short.txt
- cwen@iupui.edu 5
- Enter a file name: mbox.txt
- zqian@umich.edu 195
- """
- ''' Functions '''
- def enterFileName():
- """
- The user has to enter a filename.
- Returns fileName
- """
- fileName = None
- while fileName == None:
- # Enter filename
- try:
- fileName = input("Enter the filename: ")
- except:
- print("Invalid input!")
- continue
- return fileName
- ''' Main '''
- # Open file
- fileName = enterFileName()
- try:
- fhand = open(fileName)
- except:
- print("File not found!")
- exit()
- sender = dict()
- for line in fhand:
- words = line.split()
- # print('Debug:', words)
- if len(words) >= 2 and words[0] == 'From':
- sender[words[1]] = sender.get(words[1], 0) + 1
- # Print-out
- t = list()
- for key in sender:
- t.append((sender[key], key))
- t.sort(reverse=True)
- print(t[0][1], t[0][0])
- # Close file
- try:
- fhand.close()
- except:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement