1

I am using the following update cursor, but when I execute it, it does not error, but also makes no changes. I am trying to Update a field where any NULL, Blank, and NEW values will be updated with 'New'.

with arcpy.da.UpdateCursor("contacts", 'Contact_Status1') as cursor:
    for row in cursor:
        if  row[0] is None or ' ' or 'NEW'  :
            row[0]= "New"
            cursor.updateRow(row)

1 Answer 1

3

You have to specify the entire if test for each or, so instead of

if  row[0] is None or ' ' or 'NEW':

you need to put row[0] == with the or.

if  row[0] is None or row[0] == ' ' or row[0] =='NEW':

So in full it will look like

with arcpy.da.UpdateCursor("contacts", 'Contact_Status1') as cursor:
    for row in cursor:
        if  row[0] is None or row[0] == ' ' or row[0] =='NEW':
            row[0]= "New"
            cursor.updateRow(row)
4
  • So I was missing the if , and should of used == instead? Why the == for Blanks and Null values? Because they are not a real value? Commented Mar 30, 2017 at 19:14
  • @winscott68 Not exactly. You were missing the row[0] == for the subsequent tests. You can't just specify if myvalue = 1 or 2 or 3, you have to specify if myvalue = 1 or myvalue = 2 or myvalue = 3. In Python, an equals test requires a double =, so it would be if myvalue == 1 or myvalue == 2 or myvalue == 3. The Null (or None in Python) requires the is keyword rather than ==.
    – Midavalo
    Commented Mar 30, 2017 at 19:16
  • Ok so strings will get == and NULL requires is Commented Mar 30, 2017 at 19:58
  • @winscott68 Yes
    – Midavalo
    Commented Mar 30, 2017 at 19:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.