-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter7_exercise.py
More file actions
78 lines (67 loc) · 2.56 KB
/
Copy pathchapter7_exercise.py
File metadata and controls
78 lines (67 loc) · 2.56 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'''
Exercise 7.1
Write a program to read through a file and print the contents of the file (line by line) all in upper case.
Executing the program will look as follows:
Enter a file name: mbox-short1.txt
FROM STEPHEN.MARQUARD@UCT.AC.ZA SAT JAN 5 09:14:16 2008
RETURN-PATH: <POSTMASTER@COLLAB.SAKAIPROJECT.ORG>
RECEIVED: FROM MURDER (MAIL.UMICH.EDU [141.211.14.90])
BY FRANKENSTEIN.MAIL.UMICH.EDU (CYRUS V2.3.8) WITH LMTPA;
SAT, 05 JAN 2008 09:14:16 -0500
'''
print("<Exercise 7.1>")
fname = input("Enter file name: ")
fhandle = open(fname)
for line in fhandle:
line_after_rstrip = line.rstrip()
print(line_after_rstrip.upper())
print()
'''
Exercise 7.2
Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:
X-DSPAM-Confidence: 0.8475
Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below.
Do not use the sum() function or a variable named sum in your solution.
When you are testing below enter mbox-short2.txt as the file name.
'''
print("<Exercise 7.2>")
fname = input("Enter file name: ")
fhandle = open(fname)
values = 0
count = 0
for line in fhandle:
if not line.startswith("X-DSPAM-Confidence:") : continue
spPos = line.find(' ')
value = line[(spPos+1):]
valueInFloat = float(value)
values = values + valueInFloat
count += 1
print("Average spam confidence:", values/count)
'''
Exercise 7.3
Sometimes when programmers get bored or want to have a bit of fun,
they add a harmless Easter Egg to their program.
Modify the program that prompts the user for the file name so that it prints a funny message
when the user types in the exact file name “na na boo boo”.
The program should behave normally for all other files which exist and don’t exist.
Here is a sample execution of the program:
>>Enter the file name: mbox-short2.txt
There were 1909 subject lines in mbox.txt
>>Enter the file name: missing.tyxt
File cannot be opened: missing.tyxt
>>Enter the file name: na na boo boo
NA NA BOO BOO TO YOU - You have been punk'd!
'''
print("<Exercise 7.3>")
fname = input("Enter file name: ")
try:
if(fname == 'na na boo boo'):
print("NA NA BOO BOO TO YOU - You have been punk'd")
else:
fhandle = open(fname)
count = 0
for line in fhandle:
count += 1
print('There were', count, 'subject lines in', fname)
except:
print('File cannot be opened:', fname)