Bird
Raised Fist0

Identify the error in this code that reads a CSV file:

medium📝 Debug Q14 of Q15
Python - Structured Data Files
Identify the error in this code that reads a CSV file:
import csv
with open('file.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
    print(row)
AIndentation error in the for loop
BMissing import statement for csv module
CFile opened in wrong mode
Dcsv.reader requires newline argument
Step-by-Step Solution
Solution:
  1. Step 1: Check code indentation

    The print statement inside the for loop must be indented to be part of the loop body.
  2. Step 2: Verify other parts

    Import is present, file mode 'r' is correct for reading, and newline argument is not needed for reading.
  3. Final Answer:

    Indentation error in the for loop -> Option A
  4. Quick Check:

    Python requires correct indentation [OK]
Quick Trick: Indent inside loops to avoid syntax errors [OK]
Common Mistakes:
MISTAKES
  • Forgetting to indent inside loops
  • Thinking newline is needed for reading
  • Confusing file modes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes