Answer key · 12 questions

Python CSV Files Exercises With Answers

All 12 questions for Python CSV Files in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python CSV Files, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What does csv.DictReader return for each row?

  1. A single string
  2. An integer index
  3. A list of every column
  4. A dictionary keyed by the headercorrect

Answer: A dictionary keyed by the header

Why: The first row supplies field names, and each later row becomes a mapping.

02 · Multiple choice

What does writer.writerow do?

  1. Writes one row and a line terminatorcorrect
  2. Closes the file
  3. Sorts all rows
  4. Reads one row

Answer: Writes one row and a line terminator

Why: Each call writes one record to the output stream.

03 · Multiple choice

What does quoting=csv.QUOTE_ALL do?

  1. Sorts the fields
  2. Quotes only numbers
  3. Quotes every fieldcorrect
  4. Removes all quotes

Answer: Quotes every field

Why: QUOTE_ALL wraps every field in quotes, even when it does not contain a delimiter.

04 · Multiple choice

What does this code print?

import csv
from io import StringIO
rows = list(csv.reader(StringIO("a,b\n1,2\n")))
print(rows[1][1])
  1. a
  2. 2correct
  3. 1
  4. b

Answer: 2

Why: The second row is ['1', '2'], so index 1 is the string 2.

05 · Fill in the blank

Complete the module import.

import ____
from io import StringIO

Answer: csv

Why: The csv module provides readers and writers.

06 · Fill in the blank

Complete the reader that uses header names.

import csv
from io import StringIO
rows = list(csv.____(StringIO("name,score\nAda,92\n")))

Answer: DictReader

Why: DictReader maps each row to a dictionary.

07 · Fill in the blank

Complete the method that writes one row.

import csv
from io import StringIO
buffer = StringIO()
writer = csv.writer(buffer)
writer.____(["a", "b"])

Answer: writerow

Why: writerow writes a single record.

08 · Fill in the blank

Complete the quoting mode that quotes every field.

import csv
from io import StringIO
buffer = StringIO()
writer = csv.writer(buffer, quoting=csv.____)

Answer: QUOTE_ALL

Why: QUOTE_ALL is the all-fields quoting constant.

09 · Predict output

What does the program output?

import csv
from io import StringIO
print(next(csv.reader(StringIO("a,b\n1,2\n"))))

Answer: ['a', 'b']

Why: The first row is the list of two header strings.

10 · Predict output

What appears on the screen?

import csv
from io import StringIO
rows = list(csv.DictReader(StringIO("name,score\nAda,92\n")))
print(rows[0]["score"])

Answer: 92

Why: The score field for Ada is the string 92.

11 · Predict output

What is printed?

import csv
from io import StringIO
buffer = StringIO()
writer = csv.writer(buffer, quoting=csv.QUOTE_ALL)
writer.writerow(["a,b", "c"])
print(buffer.getvalue().strip())

Answer: "a,b","c"

Why: QUOTE_ALL wraps both fields, including the one with a comma.

12 · Predict output

What is the output?

import csv
from io import StringIO
buffer = StringIO()
writer = csv.writer(buffer)
writer.writerow(["a", "b"])
print(buffer.getvalue().strip())

Answer: a,b

Why: writerow writes the two fields separated by a comma.

Try the questions first

Open the practice page for Python CSV Files and answer the same 12 questions with instant feedback.

Practice this lesson