Challenge - 5 Problems
CSV Dictionary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Reading CSV with DictReader output
What is the output of this code snippet that reads a CSV string using csv.DictReader?
Python
import csv from io import StringIO csv_data = "name,age,city\nAlice,30,New York\nBob,25,Los Angeles" f = StringIO(csv_data) reader = csv.DictReader(f) result = [row for row in reader] print(result)
Attempts:
2 left
💡 Hint
Remember that csv.DictReader reads all values as strings.
✗ Incorrect
csv.DictReader reads each row as a dictionary with keys from the header row. All values are strings by default.
❓ Predict Output
intermediate2:00remaining
Writing CSV with DictWriter fieldnames order
What will be the content of the CSV string after running this code?
Python
import csv from io import StringIO output = StringIO() fieldnames = ['city', 'name', 'age'] writer = csv.DictWriter(output, fieldnames=fieldnames) writer.writeheader() writer.writerow({'name': 'Alice', 'age': '30', 'city': 'New York'}) writer.writerow({'name': 'Bob', 'age': '25', 'city': 'Los Angeles'}) result = output.getvalue() print(result)
Attempts:
2 left
💡 Hint
The order of columns in CSV matches the fieldnames list given to DictWriter.
✗ Incorrect
DictWriter writes columns in the order specified by fieldnames. The default line terminator is '\r\n'.
❓ Predict Output
advanced2:00remaining
Handling missing keys in DictWriter
What happens when you run this code that writes a row missing a fieldname key?
Python
import csv from io import StringIO output = StringIO() fieldnames = ['name', 'age', 'city'] writer = csv.DictWriter(output, fieldnames=fieldnames) writer.writeheader() writer.writerow({'name': 'Alice', 'city': 'New York'}) result = output.getvalue() print(result)
Attempts:
2 left
💡 Hint
Missing keys in the row dictionary are written as empty strings by default.
✗ Incorrect
DictWriter writes empty strings for missing keys in the row dictionary instead of raising errors.
❓ Predict Output
advanced2:00remaining
Reading CSV with different delimiter using DictReader
What is the output of this code that reads a semicolon-separated CSV string?
Python
import csv from io import StringIO csv_data = "name;age;city\nAlice;30;New York\nBob;25;Los Angeles" f = StringIO(csv_data) reader = csv.DictReader(f, delimiter=';') result = [row for row in reader] print(result)
Attempts:
2 left
💡 Hint
Specify the delimiter parameter to match the CSV format.
✗ Incorrect
DictReader uses the delimiter to split columns correctly. Without it, it treats the whole line as one field.
🧠 Conceptual
expert3:00remaining
Effect of restval in DictWriter on missing keys
Given this code, what is the output CSV content?
Python
import csv from io import StringIO output = StringIO() fieldnames = ['name', 'age', 'city'] writer = csv.DictWriter(output, fieldnames=fieldnames, restval='UNKNOWN') writer.writeheader() writer.writerow({'name': 'Alice', 'city': 'New York'}) result = output.getvalue() print(result)
Attempts:
2 left
💡 Hint
restval sets the default value for missing keys when writing rows.
✗ Incorrect
When a key is missing in the row dictionary, DictWriter uses restval as the value instead of empty string.