Advertisement
Mars83

9-5

Oct 9th, 2011
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 9.5
  6.    This programrecords the domain name (instead of the address) where the
  7.    message was sent from instead of who the mail came from (i.e. the whole
  8.    e-mail address). At the end of the program print out the contents of your
  9.    dictionary.
  10.    
  11.    python schoolcount.py
  12.    Enter a file name: mbox-short.txt
  13.    {'media.berkeley.edu': 4, 'uct.ac.za': 6, 'umich.edu': 7,
  14.    'gmail.com': 1, 'caret.cam.ac.uk': 1, 'iupui.edu': 8}
  15. """
  16.  
  17. ''' Functions '''
  18. def enterFileName():
  19.     """
  20.    The user has to enter a filename.
  21.    Returns fileName
  22.    """
  23.     fileName = None
  24.     while fileName == None:
  25.         # Enter filename
  26.         try:
  27.             fileName = input("Enter the filename: ")
  28.         except:
  29.             print("Invalid input!")
  30.             continue
  31.     return fileName
  32.  
  33. ''' Main '''
  34. # Open file
  35. fileName = enterFileName()
  36. try:
  37.     fhand = open(fileName)
  38. except:
  39.     print("File not found!")
  40.     exit()
  41.  
  42. domains = dict()
  43. for line in fhand:
  44.     words = line.split()
  45.     # print('Debug:', words)
  46.     if len(words) >= 2 and words[0] == 'From':
  47.         # Cut the word till '@' (inclusive)
  48.         words[1] = words[1][words[1].find('@') + 1:]
  49.         # Update dictionary
  50.         domains[words[1]] = domains.get(words[1], 0) + 1
  51.  
  52. # Print-out
  53. for i in domains:
  54.     #if domains[i] == max(domains.values()):
  55.     print(str(i) + " " + str(domains[i]))
  56.  
  57. # Close file
  58. try:
  59.     fhand.close()
  60. except:
  61.     exit()
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement