Bird
0
0

You have a CSV file with columns 'Name', 'Age', and 'City'. How can you read this file and create a list of dictionaries where each dictionary represents a row with keys as column names?

hard📝 Application Q8 of 15
Python - Structured Data Files
You have a CSV file with columns 'Name', 'Age', and 'City'. How can you read this file and create a list of dictionaries where each dictionary represents a row with keys as column names?
ARead file as text and split lines by commas
BUse csv.reader and manually map columns to dictionary keys
COpen file in write mode and use csv.writer
DUse csv.DictReader to read the file and convert rows to dictionaries
Step-by-Step Solution
Solution:
  1. Step 1: Understand csv.DictReader usage

    csv.DictReader reads CSV rows as dictionaries using header row as keys.
  2. Step 2: Identify best method for dictionary list

    Using csv.DictReader directly creates list of dicts without manual mapping.
  3. Final Answer:

    Use csv.DictReader to read the file and convert rows to dictionaries -> Option D
  4. Quick Check:

    csv.DictReader = dict rows from CSV [OK]
Quick Trick: csv.DictReader reads CSV rows as dictionaries [OK]
Common Mistakes:
  • Using csv.reader and manually creating dicts
  • Opening file in write mode when reading
  • Parsing CSV as plain text instead of using csv module

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes