File tree 2 files changed +63
-0
lines changed
2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Seeking email that sent the most emails from email logs (text file)
2
+
3
+ # 1. Request file name
4
+ fname = raw_input ('Enter file name: ' )
5
+
6
+ # 2. File handle
7
+ fhandle = open (fname )
8
+
9
+ # 3. Create dictionary
10
+ counts = dict ()
11
+
12
+ # 4. Variable: first word to look for in each sentence
13
+ x = 'From'
14
+
15
+ # 5. Loop through each sentence
16
+ for line in fhandle :
17
+ # 6. Create list
18
+ words = line .split ()
19
+
20
+ # 7. Guardian pattern for empty lines
21
+ if len (words ) < 1 :
22
+ continue
23
+
24
+ # To ignore all sentences starting from "From:"
25
+ # if words[0] == 'From:':
26
+ # continue
27
+
28
+ # 8. Ignore all other than x
29
+ if words [0 ] != x :
30
+ continue
31
+
32
+ # print words
33
+ # shows position 2 has the emails
34
+
35
+ email = words [1 ]
36
+
37
+ # 9. Map key-value pairs to dictionary
38
+ counts [email ] = counts .get (email , 0 ) + 1
39
+
40
+ # print counts
41
+ # shows you have a dictionary with key-value pairs of format email:count
42
+
43
+ # 10. Convert dictionary to list of tuples
44
+ counts = counts .items ()
45
+
46
+ # 11. Loop through lists to convert to value-key tuples
47
+ lst = list ()
48
+ for k , v in counts :
49
+ lst .append ((v , k ))
50
+
51
+ lst = sorted (lst )
52
+
53
+ # Least common
54
+ print lst [0 ]
55
+
56
+ # Most common
57
+ print lst [- 1 ]
Original file line number Diff line number Diff line change @@ -91,3 +91,9 @@ Happy scripting!
91
91
92
92
#### Requirements :
93
93
Python 3.5+
94
+
95
+ ### MostCommonEmail:
96
+ Seeking email that sent the most emails from email logs (text file)
97
+
98
+ ### Requirements:
99
+ Python 3.5+
You can’t perform that action at this time.
0 commit comments