• Home
  • Textbooks
  • The Practice of Computing Using Python
  • Files and Exceptions II

The Practice of Computing Using Python

William F. Punch, Richard Enbody

Chapter 15

Files and Exceptions II - all with Video Answers

Educators


Chapter Questions

Problem 1

A KeyBrror exception exists if you attempt to access a dictionary with an invalid key. Write a small program that illustrates using a try-except block to handle a KeyError exception. Create a small dictionary and write a special error message if an attempt is made to access the dictionary with an invalid key.

Check back soon!

Problem 2

Given dictionary D, rewrite this code using exceptions:
if $x$ in $D$ :
$\mathrm{D}[\mathrm{x}]+=1$
else:
$D[x]=1$

Check back soon!

Problem 3

Write a function that takes a string of words and integers as an argument and returns two lists: one a list of the words, the other a list of the integers. Use exceptions to distribute words and integers to their respective lists.

Check back soon!
View

Problem 4

Phone records
Telecom operators such as AT&T or Verizon maintain records of all the calls made using their network (named "call detail records," or CDRs). Usually, the CDRs are collected at the routers within the telecom networks, and millions of CDRs are stored in a single file. Although most of the records are in correct format, some records (a handful) either do not contain all the requisite fields or contain garbled data; these records cause problems during any data processing.
Consider a simplified version of a CDR that has only four fields: date and time of the call, source number, destination number, and duration of the call. Create a comma-separated file containing multiple CDRs. In that file include some erroneous or incomplete records thar:
$\bullet$ Do not have duration of the call (no duration, no billing!)
$\bullet$ Have garbled phone numbers, like 12-345-6789 or 123-45?-6789
Also, create some records in which the source or destination number is recorded as null (this happens sometimes if it was an international call). Create some records in which the comma is missing between the source and destination numbers - e.g., 123-456-7890 987-654-3210.
Note: If a comma is missing between the source and destination or they have null values, you can still get the required information and it should not be counted as an erroneous record.
Also, create another file containing all the North American area codes (see http: //www . bennetyee.org/ucsd-pages/area.html).
(a) Find the call that had the longest duration and find its source and destination regions.
(b) Find the number of garbled or incomplete CDRs.
(c) How many international calls were made?
(d) Create a list of all calls made on a particular date.
(e) Creare a list of all calls made during a particular hour.
(f) Optional: Plot a distribution of the arrival times of the calls. This plot could help in identifying busy and idle periods of the network.
Use exceptions (IndexError, ValueError, TypeError) to handle erroneous CDRs rather than checking individual cases using if-else constructs.

Victor Salazar
Victor Salazar
Numerade Educator
01:48

Problem 5

Assume that a file has opened as "somefile," such as somefile = open ('test. txt $\left.t^{\prime}, r^{\prime}\right)$. Try the following (note the " $s$ " in the second method name).
(a) What will somefile . readline () return?
(b) What will somefile readlines () return?

Foster Wisusik
Foster Wisusik
Numerade Educator

Problem 6

Given a test file named "test.txt" containing the following three lines:
Line One
Line Two
Line Three
Consider the following code:
someFile = open ('test. txt', 'r')
for line in somepile: #Loop $l$ print line
for line in someFile: * Loop 2
print line
(a) What will be printed by Loop 1?
(b) What will be printed by Loop 2?
(c) How can you modify the code so that both Loop 1 and Loop 2 print the same thing (other than having them both print nothing)?

Check back soon!
11:29

Problem 7

Suppose that a text file contains student records on each line and each record is of the format:
Name of student, student Id, GPA
Write code to read the file line by line and store all the records in lists or tuples. Hint: You need to create a list of lists or list of ruples.

RO
Reynald Oliveria
Numerade Educator

Problem 8

Write a function that takes one argument: a string file name. The function should return a list of lists such that each list is a list of the words in each line. In addition:
(a) Remove all whitespace from each word and remove all punctuation. The string module constants will be useful for the last task.
(b) Clean the words before placing them in the list. Specifically, convert all words to lowercase and throw out any "words" that contain anything other than letters.

Check back soon!