A Smarter Way to Learn Python / Chapter 67 Exercises

  • Index of exercises
  • Email me

1

2

3

4

5

6

7

8

9

10

11

12

 

Congratulations. You've aced all the exercises for this chapter.


To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to Trinket.


Post a review on Amazon for the paperback or the Kindle edition.


Email me to give me a compliment, complaint, or correction. I'll respond.


  All of my books include free interactive online exercises that make the knowledge stick.

A Smarter Way to Learn JavaScript
Shop for it at Amazon
A Smarter Way to Learn HTML and CSS
Shop for it at Amazon
A Smarter Way to Learn jQuery
Shop for it at Amazon

copyright © 2017 by Mark Myers. All rights reserved.

0,1,2,3,4,5,6,7,8,9,10,11

0

0

0

0


Before reading a CSV file, you must import the module that contains the CSV reader. Code the line that imports the module. import csv^ *import csv *$
The module you've imported is csv. You're calling the module's function named reader. The handle for the file you're reading is f. Complete the function call. contents_of_csv_file = ______________________ csv.reader(f)^ *csv\.reader\(f\) *$
Using the shorthand form, open products.csv for reading. The file handle is prods with open("products.csv") as prods: with `open`(`"`products.csv`"`) `as `prods`:
Complete the line to call the reader function in the csv module. The file handle is prods product_list = _________________csv.reader(prods)csv.reader(prods)
Call the reader function in the csv module. Make up the file handle and variable. content = csv.reader(f) ^ *[a-zA-Z_][a-zA-Z0-9_]* = csv\.reader\([a-zA-Z_][a-zA-Z0-9_]*\) *$
Open a CSV file for reading using shorthand. Then read it. Make everything up. Remember to indent the second line. with open("students.csv") as f:
  student_list = csv.reader(f)   
^ *with open\(•.+\.csv•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = csv\.reader\(\1\) *$
Call csv.reader(f). Make up the name of the variable. read_it = csv.reader(f) ^ *[a-zA-Z_][a-zA-Z0-9_]* = csv\.reader\(f\) *$
Call reader. Make everything up. x = csv.reader(f) ^ *[a-zA-Z_][a-zA-Z0-9_]* = csv\.reader\([a-zA-Z_][a-zA-Z0-9_]*\) *$
Open a CSV file for reading using shorthand. Then read it. Make everything up. with open("potions.csv") as file_to_read:
  read_file = csv.reader(file_to_read)
^ *with open\(•.+\.csv•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = csv\.reader\(\1\) *$
  1. Import the csv module.
  2. Open a CSV file for reading using shorthand.
  3. Then read it. Make everything up.
import csv
with open("potions.csv") as file_to_read:
  read_file = csv.reader(file_to_read)
^ *import csv\nwith open\(•.+\.csv•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = csv\.reader\(\1\) *$
  1. Click here to download potions.csv.
  2. Copy the file to the Desktop. We'll be using it soon.