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


Fill in the blank. with open("whatever.csv", "w", newline="") as f:
  data_handler = csv.writer(f, delimiter=",")
  _____________.writerow(["Year", "Event", "Winner"])
data_handler^ *data_handler *$
Fill in the blank. with open("whatever.csv", "w", newline="") as f:
  data_handler = csv.writer(f, delimiter=",")
  data_handler.________(["Year", "Event", "Winner"])
writerow^ *writerow *$
Fill in the blank. with open("whatever.csv", "w", newline="") as f:
  data_handler = csv.writer(f, delimiter=",")
  __________________(["Year", "Event", "Winner"])
data_handler.writerow data_handler`.`writerow
Fill in the blank. with open("whatever.csv", "w", newline="") as f:
  data_handler = csv.writer(f, delimiter=",")
  __________________["Year", "Event", "Winner"])
data_handler.writerow(data_handler.writerow(
Code the line that writes the next row. Make up the list elements. Start by indenting. with open("whatever.csv", "w", newline="") as f:
  writing_object = csv.writer(f, delimiter=",")
  writing_object.writerow(["Year", "Event", "Winner"])
  writing_object.writerow(["1997", "Wiffleball", "Sparta"]) ^ * writing_object\.writerow\(\[•.+•, •.+•, •.+•\]\) *$
Write the next line, writing a row that has four strings. Make up the elements. Start by indenting. with open("whatever.csv", "w", newline="") as csv_file:
  o = csv.writer(csv_file, delimiter=",")
  o.writerow(["Team", "City", "Mascot", "Record"]) ^ * o\.writerow\(\[•.+•, •.+•, •.+•, •.+•\]\) *$
Write the next line, writing a row that has two strings. Make up the elements. Start by indenting. with open("whatever.csv", "w", newline="") as f:
  handler = csv.writer(f, delimiter=",")
  handler.writerow(["Name", "Address"]) ^ * handler\.writerow\(\[•.+•, •.+•\]\) *$
Write the next two lines. The row has only one element. Make everything up. with open("whatever.csv", "w", newline="") as f:   info_handler = csv.writer(f, delimiter=",")
  info_handler.writerow(["Metal"])
^ * ([a-zA-Z_][a-zA-Z0-9_]*) = csv\.writer\(f, delimiter=•,•\)\n \1\.writerow\(\[•.+•\]\) *$
Import the module you need for writing to a CSV file. import csv ^ *import csv *$
Open a CSV file for writing. Write two rows to it. Each row has three elements. Make everything up. with open("planets.csv", "w", newline="") as f:
  writer = csv.writer(f, delimiter=",")
  writer.writerow(["earth", "blue", "near"])
  writer.writerow(["mars", "red", "fairly far"])  
^ *with open\(•.+\.csv•, •w•, newline=••\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n ([a-zA-Z_][a-zA-Z0-9_]*) = csv\.writer\(\1, delimiter=•,•\)\n \2\.writerow\(\[•.+•, •.+•, •.+•\]\)\n \2\.writerow\(\[•.+•, •.+•, •.+•\]\) *$
  1. In IDLE code a Python program that creates a CSV file with three rows of three elements each.
  2. Save the Python program to the Desktop and run it.
  3. Open the CSV file in IDLE.