1

I have a script that is supposed to update several fields based on the values of several other associated fields. My script is almost working as it should. However, it is overwriting my "High" and "Medium" values with "Low" despite my conditional statement. Any suggestions of where I am going wrong?

# fill polygons with "Medium", but do not overwrite those already filled with "High"
for species, primary in zip(SGCN_list, primary_habitat):
    if species not in (f.name for f in arcpy.ListFields(SGCN_FC)):
        pass
    elif primary is None:
        pass
    else:
        with arcpy.da.UpdateCursor(SGCN_FC, [species, primary]) as cursor:
            for row in cursor:
                if row[1] is not None: # if record is already populated with "High" 
                    pass
                elif row[1] >= 0.10:
                    row[0] = "Medium"
                else:
                    pass
                cursor.updateRow(row)

# fill polygons with Low
for species, secondary in zip(SGCN_list, secondary_habitat):
    if species not in (f.name for f in arcpy.ListFields(SGCN_FC)):
        pass
    elif secondary is None:
        pass
    else:
        with arcpy.da.UpdateCursor(SGCN_FC, [species, secondary]) as cursor:
            for row in cursor:
                if row[1] is not None: # if record is already populated with "High" or "Medium"
                    pass
                elif row[1] >= 0.10:
                    row[0] = "Low"
                else:
                    pass
                cursor.updateRow(row)
  • 2
    I might be missing something but is it possible that primary and secondary fields have the same value .10 or higher and therefor already have a medium value when the loop for setting low values is run? Also Isn't row[1] the field with the values and not the update field? It looks like you're "High", "Medium" and "Low" values are supposed to be in row[0] so the first condition will always or never be passed. Should it be "if row[0] is not None? – geoJshaun Aug 2 '16 at 16:50
  • Thank you Shaun! I knew it must have been something simple, and that was it. I just needed another set of eyes. My index was wrong for the first conditional pass statement within the UpdateCursor. Thanks again! – mmoore Aug 2 '16 at 16:56
  • You're welcome. Glad I could help. – geoJshaun Aug 2 '16 at 17:00
  • @Shaun you should post that as an answer – Midavalo Aug 2 '16 at 18:20
1

I might be missing something but it is possible that primary and secondary fields have the same value .10 or higher and therefor already have a medium value when the loop for setting low values is run.

Also row[1] appears to be the field with the values and not the update field.

It looks like you're "High", "Medium" and "Low" values are supposed to be in row[0] so the first condition will always or never be passed.

Then try:

if row[0] is not None:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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