Bird
Raised Fist0

What will be the output of this code snippet?

medium📝 Predict Output Q13 of Q15
Python - Structured Data Files
What will be the output of this code snippet?
import csv
with open('data.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['Name', 'Age'])
    writer.writerow(['Alice', '30'])

with open('data.csv', 'r') as f:
    reader = csv.reader(f)
    rows = list(reader)
print(rows)
A['Name', 'Age', 'Alice', '30']
BSyntaxError
C[['Name, Age'], ['Alice, 30']]
D[['Name', 'Age'], ['Alice', '30']]
Step-by-Step Solution
Solution:
  1. Step 1: Writing rows with csv.writer

    The code writes two rows: header ['Name', 'Age'] and data ['Alice', '30'] as lists.
  2. Step 2: Reading rows with csv.reader

    Reading back returns a list of lists, each inner list is a row split by commas.
  3. Final Answer:

    [['Name', 'Age'], ['Alice', '30']] -> Option D
  4. Quick Check:

    csv.reader returns list of lists [OK]
Quick Trick: csv.reader returns list of lists, not flat list [OK]
Common Mistakes:
MISTAKES
  • Expecting a flat list instead of list of lists
  • Thinking rows are single strings
  • Syntax errors from missing newline='' in open

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes