A Smarter Way to Learn Python / Chapter 68 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


Call csv.reader. Make everything up. contents = csv.reader(f)^ *[a-zA-Z_][a-zA-Z0-9_]* = csv\.reader\([a-zA-Z_][a-zA-Z0-9_]*\) *$
Declare an empty list to hold the contents of the CSV file. Make up the name of the list. file_contents = []^ *[a-zA-Z_][a-zA-Z0-9_]* = \[\] *$
Complete the fourth line. 1 with open("cats.csv") as f:
2   cat_content = csv.reader(f)
3   cats_list = []
4   for each_line in ___________
cat_content: cat_content`:
Code the first line of the for loop that loops through CSV content returned by csv.reader. The CSV content is in the variable data. The variable this_line stores each line. Don't bother to indent. for this_line in data:for this_line in data:
Code the next line. Remember to indent it. Make up the name of the variable that stores each line. Start by indenting. with open("competitions.csv") as f:
  contents = csv.reader(f)
  list = []
  for a_line in contents: ^ * for [a-zA-Z_][a-zA-Z0-9_]* in contents: *$
Code the next line. Start by double-indenting. with open("competitions.csv") as f:
  contents = csv.reader(f)
  list = []
  for a_line in contents:
    list += a_line ^ * list \+= a_line *$
Code the next two lines. Make up the name of the variable that stores each line. Indent correctly. with open("products.csv") as file_to_read:
  x = csv.reader(file_to_read)
  y = []
  for z in x:
    y += z
^ * for ([a-zA-Z_][a-zA-Z0-9_]*) in x:\n y \+= \1 *$
Code the missing line. Start by indenting. with open("products.csv") as file_to_read:
  x = csv.reader(file_to_read)

  for z in x:
    y += z
  y = [] ^ * y = \[\] *$
The file is open. The file handle is f. Code the next four lines. Make everything up. Start by indenting.   x = csv.reader(f)
  y = []
  for z in x:
    y += z
^ * ([a-zA-Z_][a-zA-Z0-9_]*) = csv\.reader\(f\)\n ([a-zA-Z_][a-zA-Z0-9_]*) = \[\]\n for ([a-zA-Z_][a-zA-Z0-9_]*) in \1:\n \2 \+= \3 *$
Open a CSV file and read it. That's five lines of code. Make everything up. with open("oceans.csv") as file_to_read:
  x = csv.reader(file_to_read)
  y = []
  for z in x:
    y += z
^ *with open\(•.+\.csv•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n ([a-zA-Z_][a-zA-Z0-9_]*) = csv\.reader\(\1\)\n ([a-zA-Z_][a-zA-Z0-9_]*) = \[\]\n for ([a-zA-Z_][a-zA-Z0-9_]*) in \2:\n \3 \+= \4 *$
  1. If you completed the exercises for the previous chapter, the file potions.csv is on the Desktop. Check to be sure it's there. If it isn't there, click here to download it, then copy the file to the Desktop.
  2. In IDLE code a Python program.
  3. Import csv.
  4. Open potions.csv for reading.
  5. Define an empty list.
  6. Read the file.
  7. Loop through the content, adding each line to the list.
  8. Display the list. (This line isn't indented.)