0

I'm having the 'TypeError: argument of type 'NoneType' is not iterable and adding an exception doesn't seem to work' error that I've seen elsewhere on here and have found from other answers that it's due to the loop returning a None value but adding a None exception doesn't work and I can't see how else to do this.

I'm using openpyxl with Python3 and have subjects in column A that are updated through another project so could have 0 to multiple lines (I unmerge them all at the start of the script). I need to search all of the rows for each project in column J for dates to copy across to another column, and as column 2 is blank for rows that have been updated I use a for loop with a if statement to add blank rows to an array for later deletion.

All rows that aren't blank, I use the else statement to count in tempList how many blank rows are in that project and then for loop to set myStringTest as the column J value so I can search for the text I want - if I find it I want to copy a slice of the text (a date) across to another column - I just have print('test') here at the moment.

for i in range(2, sheet.max_row + 1):
if sheet.cell(row=i, column=1).value == None:
    print('This row has nothing in it')
    arrayForDeletion2.append(i)
else:
    tempList = 1
    x = i
    while sheet.cell(row=x+1, column=1).value == None:
        tempList += 1
        x += 1
        if tempList >30:
            break
    print(i)
    print(tempList)
    for y in range(i, i+tempList):
        myStringTest = (sheet.cell(row=i, column=10).value)
        if 'Original' in myStringTest and not None:
            print('test')
        i += 1

>>>Traceback (most recent call last):
  File "main.py", line 113, in <module>
  if 'Original' in myStringTest and not None:
  TypeError: argument of type 'NoneType' is not iterable

So A2 may have 6 update rows, with J4, J5 and J7 each having a string saying 'Original target date: XX/XX/XX' - I'm looking to recognise that in the if statement and if so to copy out just the date to another column. If no string, do nothing.

That's probably not very clear sorry but I'm fairly new to python and have looked around a lot for an answer but can't work this out. Really grateful for any help. Thanks.

Luke
  • 37
  • 7

1 Answers1

1

If sheet.cell(r,c).value returns None, you are testing 'Original' in None, effecively <str> in <NoneType>. Since an implicit cast (of <NoneType> to <str>) is not available, python throws an error. You've tried to catch this using and None, but this code is not evaluated how you expected it to. Try the below, you'll get the same error:

p = None
if 'test' in p:
   print(p)

Moving the None check to a separate statement before the in should solve the issue. Python raises the error, so your application can execute fallback code that corrects unintended behaviour or notifies the user about faulty input. If you want to catch and handle it locally, use a try/except block:

p = None
try:
    if 'lol' in p:
        print('p')
    else:
        print('none')
except TypeError:
    print('Code to be executed if p is None goes here.')

Incidentally, the below prints p, because not None always evaluates to True. You will need an explicit myStringTest is not None, in which the is keyword triggers the comparison.

p = None
if not None:
    print('p')
else:
    print('none')

Lastly, a recommendation. Semantically, x == None tests whether x is equivalent to the None object.

But there is only one None object, which is the None object. So the test you would like to execute uses the is keyword: x is None, which checks whether x and None refer to the same object.

How does this matter? Well, part of it is style. That leads to discussions like these. But part of it is functionality. This comments on readability, this remarks on a cool edge case: != | == use the __eq__() method of a class and you do not know how it is implemented. If I want to know whether x and y refer to the same object, is guarantees proper behaviour.

MvZ
  • 231
  • 1
  • 5
  • Thanks for the SambalMinion - really helpful, and got it sorted now. Thanks for extra links for the semantic stuff too, will have a read through. – Luke Aug 13 '18 at 18:26