Challenge - 5 Problems
CSV Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Reading CSV with csv.reader
What is the output of this code snippet that reads a CSV string using
csv.reader?Python
import csv from io import StringIO csv_data = "name,age\nAlice,30\nBob,25" file_like = StringIO(csv_data) reader = csv.reader(file_like) rows = list(reader) print(rows)
Attempts:
2 left
💡 Hint
csv.reader returns each row as a list of strings.
✗ Incorrect
csv.reader reads CSV data line by line and returns each row as a list of strings. So the output is a list of lists representing rows.
❓ Predict Output
intermediate2:00remaining
Writing CSV with csv.DictWriter
What is the output of this code that writes a list of dictionaries to CSV format using
csv.DictWriter?Python
import csv from io import StringIO output = StringIO() fieldnames = ['name', 'age'] writer = csv.DictWriter(output, fieldnames=fieldnames) writer.writeheader() writer.writerow({'name': 'Alice', 'age': 30}) writer.writerow({'name': 'Bob', 'age': 25}) print(output.getvalue())
Attempts:
2 left
💡 Hint
csv.DictWriter writes CSV rows as strings with headers.
✗ Incorrect
csv.DictWriter writes the header row first, then each dictionary as a CSV row with values converted to strings.
❓ Predict Output
advanced2:00remaining
Reading CSV with custom delimiter
What is the output of this code that reads CSV data with a semicolon delimiter?
Python
import csv from io import StringIO csv_data = "name;age\nAlice;30\nBob;25" file_like = StringIO(csv_data) reader = csv.reader(file_like, delimiter=';') rows = list(reader) print(rows)
Attempts:
2 left
💡 Hint
Specify the delimiter parameter to split columns correctly.
✗ Incorrect
By setting delimiter=';', csv.reader splits each line by semicolon, returning lists of values.
❓ Predict Output
advanced2:00remaining
Writing CSV with quoting
What is the output of this code that writes CSV data with quoting for all fields?
Python
import csv from io import StringIO output = StringIO() fieldnames = ['name', 'note'] writer = csv.DictWriter(output, fieldnames=fieldnames, quoting=csv.QUOTE_ALL) writer.writeheader() writer.writerow({'name': 'Alice', 'note': 'Hello, world!'}) writer.writerow({'name': 'Bob', 'note': 'Goodbye'}) print(output.getvalue())
Attempts:
2 left
💡 Hint
csv.QUOTE_ALL adds quotes around every field.
✗ Incorrect
With quoting=csv.QUOTE_ALL, every field including headers is enclosed in double quotes.
🧠 Conceptual
expert2:00remaining
csv.DictReader behavior without header row
What is the output of this code when reading CSV data without a header row using
csv.DictReader?Python
import csv from io import StringIO csv_data = "Alice,30\nBob,25" file_like = StringIO(csv_data) reader = csv.DictReader(file_like) rows = list(reader) print(rows)
Attempts:
2 left
💡 Hint
csv.DictReader uses the first row as header by default.
✗ Incorrect
csv.DictReader uses the first row ['Alice', '30'] as fieldnames if none provided. The second row ['Bob', '25'] maps to {'Alice': 'Bob', '30': '25'}.