Editor

Predict the DictReader Output

Beginner · Predict the output · from Python CSV: Read, Write, and Process CSV Files

From the lesson: Python CSV: Read, Write, and Process CSV Files

Think about a spreadsheet with rows and columns - names in one column, ages in another, scores in a third. A CSV file (Comma-Separated Values) is just a plain text version of that spreadsheet, where commas separate each column.

Read the lesson

What does this code print? Think about how DictReader maps column names to values.

import csv
import io

data = 'color,count\nred,3\nblue,7'
f = io.StringIO(data)
reader = csv.DictReader(f)

for row in reader:
    print(row['count'])
import csv
import io
data = 'color,count\nred,3\nblue,7'
f = io.StringIO(data)
reader = csv.DictReader(f)
for row in reader:
print(row['count'])

More CSV exercises

All Python exercises · 50 practice problems · File I/O & Data Formats quiz