-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise9_3.py
27 lines (25 loc) · 1.35 KB
/
exercise9_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#Exercise 9.3: Write a program to read through a mail log, build a histogram
#using a dictionary to count how many messages have come from each email address, and print the dictionary.
#Enter file name: mbox-short.txt
#{'[email protected]': 2, '[email protected]': 3,
#'[email protected]': 3, '[email protected]': 1,
#'[email protected]': 1, '[email protected]': 1,
#'[email protected]': 4, '[email protected]': 1}
dictionary_addresses=dict () # Define the dictionary_addresses variable
fname=input ('Enter file name:') # Prompt the user to Enter file name.
try: # Start the try-except loop.
fhand =open (fname)
except FileNotFoundError: # If the file is not found print 'File cannot be opened'
print ('File cannot be opened:', fname)
(exit )
for line in fhand:
words=line.split () # This command aids in reading lines
if len (words) <2 or [words [0]] !='From':# This seeks the 'From' string in all the sent mails.
continue
else:
if words [1] not in dictionary_addresses:
dictionary_addresses [words[1]]=1
else:
dictionary_addresses [words[1]]=+1
print (dictionary_addresses)