0
0
Pythonprogramming~20 mins

Dictionary-based CSV handling in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSV Dictionary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ASyntaxError
B[{'name': 'Alice', 'age': 30, 'city': 'New York'}, {'name': 'Bob', 'age': 25, 'city': 'Los Angeles'}]
C[{'name': 'Alice', 'age': '30', 'city': 'New York'}, {'name': 'Bob', 'age': '25'}]
D[{'name': 'Alice', 'age': '30', 'city': 'New York'}, {'name': 'Bob', 'age': '25', 'city': 'Los Angeles'}]
Attempts:
2 left
💡 Hint
Remember that csv.DictReader reads all values as strings.
Predict Output
intermediate
2: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)
A
name,age,city
Alice,30,New York
Bob,25,Los Angeles
B
city,name,age
New York,Alice,30
Los Angeles,Bob,25
C
city,name,age
New York,Alice,30
Los Angeles,Bob,25
DTypeError
Attempts:
2 left
💡 Hint
The order of columns in CSV matches the fieldnames list given to DictWriter.
Predict Output
advanced
2: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)
AKeyError
B
name,age,city
Alice,0,New York
C
name,age,city
Alice,,New York
DTypeError
Attempts:
2 left
💡 Hint
Missing keys in the row dictionary are written as empty strings by default.
Predict Output
advanced
2: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)
A[{'name': 'Alice', 'age': '30', 'city': 'New York'}, {'name': 'Bob', 'age': '25', 'city': 'Los Angeles'}]
B[{'name;age;city': 'Alice;30;New York'}, {'name;age;city': 'Bob;25;Los Angeles'}]
C[]
DValueError
Attempts:
2 left
💡 Hint
Specify the delimiter parameter to match the CSV format.
🧠 Conceptual
expert
3: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)
A
name,age,city
Alice,UNKNOWN,New York
B
name,age,city
Alice,,New York
CKeyError
DTypeError
Attempts:
2 left
💡 Hint
restval sets the default value for missing keys when writing rows.