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


Rewrite this line to add rows to a CSV file. with open("x.csv", "w", newline="") as f: with open("x.csv", "a", newline="") as f:^ *with open\(•x\.csv•, •a•, newline=••\) as f: *$
Open a CSV file to add rows. Make up the filename and file handle. with open("x.csv", "a", newline="") as f:^ *with open\(•.+\.csv•, •a•, newline=••\) as [a-zA-Z_][a-zA-Z0-9_]*: *$
Fill in the blank. with open("x.csv", "a", newline="") as f:
  data_handler = _____________________________
csv.writer(f, delimiter=",") csv`.writer`(`f`, `delimiter`=`"`,`"`)
Code the next line. Name the data handler o. Start by indenting. with open("provinces.csv", "a", newline="") as csv_file:
o = csv.writer(csv_file, delimiter=",") o = csv.writer(csv_file, delimiter=",")
Code the first two lines to open a CSV file for appending. Make everything up. with open("provinces.csv", "a", newline="") as csv_file:
  o = csv.writer(csv_file, delimiter=",")
^ *with open\(•.+\.csv•, •a•, newline=••\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = csv\.writer\(\1, delimiter=•,•\) *$
Code the next line to append a row containing two strings. Make everything up. Start by indenting. with open("provinces.csv", "a", newline="") as csv_file:
  o = csv.writer(csv_file, delimiter=",")
  o.writerow(["Ontario", "14"]) ^ * o\.writerow\(\[•.+•, •.+•\]\) *$
The name of a CSV file has been assigned to the variable target_file. Open it for appending. Make up the file handle. with open(target_file, "a", newline="") as f: ^ *with open\(target_file, •a•, newline=••\) as [a-zA-Z_][a-zA-Z0-9_]*: *$
Code the first three lines to read a CSV file. Use shorthand. Make everything up. with open("xyz.csv") as f:
  stuff_to_read = csv.reader(f)
  rows = []
^ *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_]* = \[\] *$
Code all five lines to read a CSV file. Use shorthand. Make everything up. with open("xyz.csv") as f:
  stuff_to_read = csv.reader(f)
  rows = []
  for each_row in stuff_to_read:
    rows += each_row
^ *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 *$
Code the first three rows to append a row to a CSV file. The row contains three strings. Make everything up. with open("provinces.csv", "a", newline="") as f:
  o = csv.writer(f, delimiter=",")
  o.writerow(["Labrador", "East", "Moderate"])
^ *with open\(•.+\.csv•, •a•, newline=••\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n ([a-zA-Z_][a-zA-Z0-9_]*) = csv\.writer\(\1, delimiter=•,•\)\n \2\.writerow\(\[•.+•, •.+•, •.+•\]\) *$
  1. In IDLE code a Python program that appends a row to one of the CSV files you've already created.
  2. Save the Python program to the Desktop and run it.
  3. Open the CSV file in IDLE.