Bird
0
0

You want to read a CSV file where the first row contains column names, and then write a new CSV file with only rows where the 'Age' column is greater than 25. Which approach is correct?

hard📝 Application Q15 of 15
Python - Structured Data Files
You want to read a CSV file where the first row contains column names, and then write a new CSV file with only rows where the 'Age' column is greater than 25. Which approach is correct?
AUse csv.reader to read, skip first row manually, filter rows, then write with csv.writer.
BUse csv.writer to read and write files directly without filtering.
CUse csv.reader to read all rows, convert 'Age' to int, then write with csv.DictWriter without header.
DUse csv.DictReader to read rows as dictionaries, filter by 'Age' key, then write with csv.DictWriter including header.
Step-by-Step Solution
Solution:
  1. Step 1: Choose reading method with headers

    csv.DictReader reads CSV rows as dictionaries using the first row as keys, making it easy to filter by column names like 'Age'.
  2. Step 2: Filter and write with headers

    Filter rows where 'Age' > 25, then write using csv.DictWriter with fieldnames to include headers properly.
  3. Final Answer:

    Use csv.DictReader to read rows as dictionaries, filter by 'Age' key, then write with csv.DictWriter including header. -> Option D
  4. Quick Check:

    DictReader + DictWriter for header and filtering [OK]
Quick Trick: Use DictReader/DictWriter for header-based filtering [OK]
Common Mistakes:
  • Skipping header manually instead of using DictReader
  • Writing without headers causing missing columns
  • Using csv.writer without filtering logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes