Bird
0
0

What is wrong with this code snippet for reading Excel data?

medium📝 Debug Q14 of 15
Selenium Python - Data-Driven Testing
What is wrong with this code snippet for reading Excel data?
from openpyxl import load_workbook
wb = load_workbook('testdata.xlsx')
sheet = wb['Sheet1']
for row in sheet.iter_rows(min_row=2, values_only=True):
    print(row[0])
ANo error; code reads first column from second row correctly
BUsing sheet['Sheet1'] instead of wb['Sheet1']
CNot specifying min_row=1 in iter_rows
DMissing call to wb.save() before reading
Step-by-Step Solution
Solution:
  1. Step 1: Check sheet access

    wb['Sheet1'] correctly accesses the sheet named 'Sheet1'.
  2. Step 2: Analyze iter_rows usage

    iter_rows with min_row=2 and values_only=True returns tuples of cell values from second row onward.
  3. Step 3: Confirm print statement

    Printing row[0] prints the first column value of each row starting from row 2.
  4. Final Answer:

    No error; code reads first column from second row correctly -> Option A
  5. Quick Check:

    Code reads data correctly [OK]
Quick Trick: wb['Sheet1'] accesses sheet; iter_rows returns values [OK]
Common Mistakes:
  • Using sheet['Sheet1'] instead of wb['Sheet1']
  • Forgetting values_only=True to get cell values
  • Thinking wb.save() is needed before reading

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes