Advertisement
Mars83

10-1

Oct 9th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 10.1
  6.    Revise a previous program as follows: Read and parse the β€œFrom” lines and
  7.    pull out the addresses from the line. Count the number of messages from
  8.    each person using a dictionary.
  9.    After all the data has been read print the person with the most commits by
  10.    creating a list of (count, email) tuples from the dictionary and then
  11.    sorting the list in reverse order and print out the person who has the most
  12.    commits.
  13.    
  14.    Sample Line:
  15.    From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
  16.    
  17.    Enter a file name: mbox-short.txt
  18.    cwen@iupui.edu 5
  19.    
  20.    Enter a file name: mbox.txt
  21.    zqian@umich.edu 195
  22. """
  23.  
  24. ''' Functions '''
  25. def enterFileName():
  26.     """
  27.    The user has to enter a filename.
  28.    Returns fileName
  29.    """
  30.     fileName = None
  31.     while fileName == None:
  32.         # Enter filename
  33.         try:
  34.             fileName = input("Enter the filename: ")
  35.         except:
  36.             print("Invalid input!")
  37.             continue
  38.     return fileName
  39.  
  40. ''' Main '''
  41. # Open file
  42. fileName = enterFileName()
  43. try:
  44.     fhand = open(fileName)
  45. except:
  46.     print("File not found!")
  47.     exit()
  48.  
  49. sender = dict()
  50. for line in fhand:
  51.     words = line.split()
  52.     # print('Debug:', words)
  53.     if len(words) >= 2 and words[0] == 'From':
  54.         sender[words[1]] = sender.get(words[1], 0) + 1
  55.  
  56. # Print-out
  57. t = list()
  58. for key in sender:
  59.     t.append((sender[key], key))
  60.     t.sort(reverse=True)
  61. print(t[0][1], t[0][0])
  62.  
  63. # Close file
  64. try:
  65.     fhand.close()
  66. except:
  67.     exit()
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement