Python for Everybody
Chapter 7
Exercise 7.1
"""
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:
python shout.py
enter a file name: mbox.short.txt
FROM STEPHEN.MARQUARD@UTC.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
You can download the file from
www.py4e.com/code3/mbox-short.txt
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 31, 2017
"""
# If in a unix-like environment, you can download mbox-short.txt with the
# following command,
# $ curl -O https://www.py4e.com/code3/mbox-short.txt
fhand = open('mbox-short.txt')
for line in fhand: # Handles one line at a time
shout = line.rstrip().upper() # Removes newline and capitalizes
print(shout)
Exercise 7.2
"""
Exercise 7.2: Write a program to prompt for a file name, and then read
through the file and look for lines of the form:
X-DSPAM-Confidence:0.8475
When you encounter a line that starts with "X-DSPAM-Confidence:" pull apart
the line to extract the floating-point number on the line. count these lines
and then compute the total of the spam confidence values from these lines.
When you reach the end of the file, print out the average spam confidence.
Enter the file name: mbox.txt
Average spam confidence: 0.894128046745
Enter the file name: mbox-short.txt
Average spam confidence: 0.750718518519
Test your file on the mbox.txt and mbox-short.txt files.
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 31, 2017
"""
count = 0 # Initialize variables
total = 0
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except FileNotFoundError:
print('File cannot be opened: ', fname)
quit()
for line in fhand:
if line.startswith('X-DSPAM-Confidence: '):
count = count + 1
colpos = line.find(':')
number = line[colpos+1:].strip() # Removes all text except number
SPAM_C = float(number)
total = total + SPAM_C
average = total / count
print('Average spam confidence: ', average)
Exercise 7.3
"""
Exercise 7.3: Sometimes when programmers get vored or want to have a bit of
fun, they adda harmless Easter Egg to their program. Modify the program that
prompts the user for a file name so that is prints a funny message when the
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 exit. Here is a
sample execution of the program:
python egg.py
Enter the file name: mbox.txt
There were 1797 subject lines in mbox.txt
python egg.py
Enter the file name: missing.tyxt
File cannot be opened: missing.tyxt
python egg.py
Enter the file name: na na boo boo
NA NA BOO BOO TO YOU - You have been punk'd!
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
Solution by Jamison Lahman, May 31, 2017
"""
fname = input('Enter the file name: ')
try:
if fname == 'na na boo boo':
print('NA NA BOO BOO TO YOU - You have been punk\'d!')
exit()
fhand = open(fname)
except FileNotFoundError:
print('File cannot be opened:', fname)
exit()
count = 0
for line in fhand:
if line.startswith('Subject:'):
count += 1
print('There were', count, 'subject lines in', fname)