0

I was working on reading some data from excel. However, i have countered a simple if statement. Not quite sure what have i missed. When i run my code, it will not go into the if statement but it will instead jump to else statement even though the value specified is exactly the same. below is my code. Thank you in advance!

from openpyxl import load_workbook
wb = load_workbook()
sheet_ranges = wb['Sheet1']
xd_date_count = 3
cookie = sheet_ranges['A' + str(xd_date_count)].value # i have a value "None" stored in A3 in this excel
print(cookie) # when i print this, it prints out None
if cookie == "None":
    print("ok")
else:
    print("no")
3
  • 1
    Are you sure that your cookie is "None" and not None? The former would happen for a cell with the literal text "None", whereas the latter would happen for an empty cell, probably Commented Apr 7, 2021 at 15:06
  • 1
    try printing print(repr(cookie), repr("None")) to check what Green Cloak Guy suggested Commented Apr 7, 2021 at 15:06
  • oh i see what you mean now, Thank you for the clarity Commented Apr 7, 2021 at 15:12

1 Answer 1

1

If you are looking for a None object, you should try None in stead of "None", as this will be interpreted as a string.

In addition

if cookie is None:

is nicer to read, but that's just a personal flavor thing

Sign up to request clarification or add additional context in comments.

3 Comments

'Just a wild guess' shouldn't be an answer. This is more a comment.
Agree. Changed the text.
With the brackets around the word None, you where literally looking for the string "None", while this is a None object which is not a string and therefore be checked using None.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.