0

I have the following line of code in Python:

if (sheet1.cell_value(i,8).lower()==sheet2.cell_value(0,j).lower() or (if hasattr(sheet2.cell_value(2,j), 'lower'): if sheet1.cell_value(i,8).lower()==sheet2.cell_value(2,j).lower())):

I currently get a SyntaxError: invalid syntax error

The reason I try to use this or, if statements is that sheet2.cell_value(2,j) may not have a value in Excel (in which case it would be #VALUE!) in Excel. Thus, the second if in the or sheet1.cell_value(i,8).lower()==sheet2.cell_value(2,j).lower())): must be evaluated only in case there is a value in the cell.. How can I fix that? Thanks

3
  • You evidently have parentheses problems, but, what do you expect the colon in the middle to do? Commented Mar 25, 2015 at 15:02
  • what do you mean? can you be more specific? I think you need the : in ifs in Python... Commented Mar 25, 2015 at 15:05
  • Consider posting a simple example we can run... often times you answer your own question that way. For instance if True or if False: pass demonstrates the problem nicely. Commented Mar 25, 2015 at 15:23

2 Answers 2

1

First, a little bit of formatting wouldn't hurt. You don't have to smash your if statement onto one line. Additionally, the way you have it now is invalid. Break your ifs onto separate lines

Second, you have parenthesis issues. Currently, the parenthesis are going across statements (through the colon).

This block aligns the parenthesis. I removed the ones surrounding the entire statement. Instead, we have your first or conditional. If that evaluates to True, then we do your second equality check. If that passes, the rest of your logic goes into that block.

if sheet1.cell_value(i,8).lower()==sheet2.cell_value(0,j).lower() or  hasattr(sheet2.cell_value(2,j), 'lower'): 
    if sheet1.cell_value(i,8).lower()==sheet2.cell_value(2,j).lower():
        # Do stuff here
Sign up to request clarification or add additional context in comments.

2 Comments

I still get an error with your code: if sheet1.cell_value(i,8).lower()==sheet2.cell_value(0,j).lower() or (if hasattr(sheet2.cell_value(2,j), 'lower')): ^ SyntaxError: invalid syntax
@adrCoder, I missed another if in there. Check the update to see. You need to remove the or (if...) so that it is only or hasattr(...)
0

Writing (if ...) or if ...: if ... are both forbidden in Python. If you want to write "condition1, and, if that's true, also condition2", just write condition1 and condition2 (in which case condition2 will not be evaluated if condition1 is false). Thus:

if (sheet1.cell_value(i,8).lower()==sheet2.cell_value(0,j).lower() or (hasattr(sheet2.cell_value(2,j), 'lower') and sheet1.cell_value(i,8).lower()==sheet2.cell_value(2,j).lower())):

Comments

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.