Select to view content in your preferred language

Update cursor stops

592
5
01-06-2023 12:15 PM
CCWeedcontrol
Regular Contributor

I am trying the following but I only get 25 in the progress field on features that have something in "Field5". It seems to stop if it encounters a blank record in Field5. What am i doing wrong?

 

valDict{}

with arcpy.da.UpdateCursor(table2, ["OBJECTID","Field1","Field2","Field3","Field4","Field5","Field6", "Field7", "Field8","Progress"],
                           where) as cursor:
    for row in cursor:
            try:
                if row[5] is None: #if row[5] not in (None, "", " "):
                    continue
                if row[5] in list1:
                    print ("{} is in list".format(row[0]))
                    row[9] = valDict.get(row[5]) if row[6] == "Pass" else valDict.get(row[5], 25) - 25 
                cursor.updateRow(row)
            except:
                continue

 

 

 

 

0 Kudos
5 Replies
ChrisJRoss13
New Contributor III

I believe there are a few things wrong with your code.

  1. The first line, "valDict{}", is incomplete. It looks like it's supposed to be creating an empty dictionary, but it's missing the "=", so it will throw a syntax error. It should be written as "valDict = {}".

  2. The "where" clause in the UpdateCursor is not defined. It should be replaced with a valid SQL WHERE clause, such as "WHERE Field1 = 'some value'"

  3. The "continue" statement in the "except" block will prevent any errors from being raised or logged. Instead, it would be more helpful to either log the error or raise it to be handled by an enclosing try/except block.

See below for assistance:

valDict = {}

with arcpy.da.UpdateCursor(table2, ["OBJECTID","Field1","Field2","Field3","Field4","Field5","Field6", "Field7", "Field8","Progress"], "WHERE Field1 = 'some value'") as cursor:
for row in cursor:
try:
if row[5] is None: #if row[5] not in (None, "", " "):
continue
if row[5] in list1:
print ("{} is in list".format(row[0]))
row[9] = valDict.get(row[5]) if row[6] == "Pass" else valDict.get(row[5], 25) - 25
cursor.updateRow(row)
except Exception as e:
print(e)

0 Kudos
CCWeedcontrol
Regular Contributor

The ValDict and the where do have values, I was just trying to post quickly and didn't include them.

I did try the exception as e: but no message gets printed.

0 Kudos
CCWeedcontrol
Regular Contributor

Both row[5] and row[6] have blanks in the fields.

0 Kudos
RhettZufelt
MVP Notable Contributor

At a quick glance, it appears you have indentation issues, and some missing colons.

the if row[5] line has another if statement in it, without colon or what to do if true.  else statement also wrong indentation and no colon.

Don't know if this is the issue, but would expect it to be throwing syntax errors.

valDict{}

with arcpy.da.UpdateCursor(table2, ["OBJECTID","Field1","Field2","Field3","Field4","Field5","Field6", "Field7", "Field8","Progress"],
                           where) as cursor:
    for row in cursor:
            try:
                if row[5] is None: #if row[5] not in (None, "", " "):
                    continue
                if row[5] in list1:
                    print ("{} is in list".format(row[0]))
                    row[9] = valDict.get(row[5]) 
                if row[6] == "Pass":
                     missing what to do if true
                else:
                     valDict.get(row[5], 25) - 25 
                cursor.updateRow(row)
            except:
                continue

R_

0 Kudos
by Anonymous User
Not applicable
valDict.get(row[5], 25)

will try to get the key (row[5]) from the dictionary valDict. If there is no key in the dictionary, it returns the default value of 25.

I take it this question is is part of your other question you posted a couple weeks ago and if that is the case, you should continue in that thread because what you are trying to do with the 'decision tree' to assign percent complete is a lot more complex than this code example can handle.

0 Kudos