0
0
Pythonprogramming~20 mins

Reading and writing CSV data in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSV Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A['name,age', 'Alice,30', 'Bob,25']
B[{'name': 'Alice', 'age': '30'}, {'name': 'Bob', 'age': '25'}]
C[['name', 'age'], ['Alice', '30'], ['Bob', '25']]
DSyntaxError
Attempts:
2 left
💡 Hint
csv.reader returns each row as a list of strings.
Predict Output
intermediate
2: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())
Aname,age\nAlice,30\nBob,25\n
B['name', 'age']\n['Alice', '30']\n['Bob', '25']
C{'name': 'Alice', 'age': 30}\n{'name': 'Bob', 'age': 25}
DTypeError
Attempts:
2 left
💡 Hint
csv.DictWriter writes CSV rows as strings with headers.
Predict Output
advanced
2: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)
A[['name;age'], ['Alice;30'], ['Bob;25']]
BValueError
C[{'name': 'Alice', 'age': '30'}, {'name': 'Bob', 'age': '25'}]
D[['name', 'age'], ['Alice', '30'], ['Bob', '25']]
Attempts:
2 left
💡 Hint
Specify the delimiter parameter to split columns correctly.
Predict Output
advanced
2: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())
Aname,note\nAlice,Hello, world!\nBob,Goodbye\n
B"name","note"\n"Alice","Hello, world!"\n"Bob","Goodbye"\n
C['name', 'note']\n['Alice', 'Hello, world!']\n['Bob', 'Goodbye']
DSyntaxError
Attempts:
2 left
💡 Hint
csv.QUOTE_ALL adds quotes around every field.
🧠 Conceptual
expert
2: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)
A[{'Alice': 'Bob', '30': '25'}]
BValueError
C[{'fieldnames': None}, {'fieldnames': None}]
D[{'Alice': '30'}, {'Bob': '25'}]
Attempts:
2 left
💡 Hint
csv.DictReader uses the first row as header by default.