setValue not working in arcpy UpdateCursor

3749
3
Jump to solution
02-19-2017 12:18 PM
jameshickey
Occasional Contributor

I am try to iterate over 3 feature datasets in 1 gdb and update a field value in all of the feature classes to "No".

ArcMap 10.4.1 Standard license. Windows 7

Here is my code. 

import arcpy
arcpy.env.workspace = r"PATH HERE"

#list of datasets
datasets = ["WaterDistribution", "WastewaterSystem", "Stormwater"]

#field to update
updateField = "AP_CHECKED"
#value to enter into field
value = "No"

for ds in datasets:
 for fc in arcpy.ListFeatureClasses("","",ds):
 print("Working on DS: {0}, FC: {1}" .format(ds, fc))
 cursor = arcpy.UpdateCursor(fc, updateField)
 for row in cursor:
 row.setValue(updateField, value)
 cursor.updateRow(row)

The code runs in the Idle window and the Datasets and FCs are listed from the print line but none of the values change in the db. 

Is the code correct?

Thanks,

James

0 Kudos
1 Solution

Accepted Solutions
jameshickey
Occasional Contributor

I was able to get the code to work. Starting an edit session and using arcpy.da.UpdateCursor rather than the arcpy.UpdateCursor seemed to do the trick.

Sorry about the code snippet in the previous post not being formatted correctly. I just figured out how to do that.

import arcpy
arcpy.env.workspace = r"PATH TO GDB HERE"
ws = r"PATH TO GDB HERE"
#list of datasets
datasets = ["WaterDistribution", "WastewaterSystem", "Stormwater"]

#field to update
updateField = "AP_CHECKED"
#value to enter into field
value = "No"

edit = arcpy.da.Editor(ws)
edit.startEditing(False, True)
edit.startOperation()

for ds in datasets:
    for fc in arcpy.ListFeatureClasses("","",ds):
        print("Working on DS: {0}, FC: {1}" .format(ds, fc))
        with arcpy.da.UpdateCursor(fc, updateField) as cursor:
            for row in cursor:
                row[0] = value
                cursor.updateRow(row)
                
edit.stopOperation()
edit.stopEditing(True)

View solution in original post

3 Replies
DanPatterson_Retired
MVP Emeritus

You will need to format you code using the syntax highlighter (follow the ... 's) so that your indentation can be checked

0 Kudos
jameshickey
Occasional Contributor

I was able to get the code to work. Starting an edit session and using arcpy.da.UpdateCursor rather than the arcpy.UpdateCursor seemed to do the trick.

Sorry about the code snippet in the previous post not being formatted correctly. I just figured out how to do that.

import arcpy
arcpy.env.workspace = r"PATH TO GDB HERE"
ws = r"PATH TO GDB HERE"
#list of datasets
datasets = ["WaterDistribution", "WastewaterSystem", "Stormwater"]

#field to update
updateField = "AP_CHECKED"
#value to enter into field
value = "No"

edit = arcpy.da.Editor(ws)
edit.startEditing(False, True)
edit.startOperation()

for ds in datasets:
    for fc in arcpy.ListFeatureClasses("","",ds):
        print("Working on DS: {0}, FC: {1}" .format(ds, fc))
        with arcpy.da.UpdateCursor(fc, updateField) as cursor:
            for row in cursor:
                row[0] = value
                cursor.updateRow(row)
                
edit.stopOperation()
edit.stopEditing(True)
JoshuaBixby
MVP Esteemed Contributor

ArcPy Editor is part of the ArcPy Data Access module, which explains why you have to use the Data Access cursors instead of the older/original ArcPy cursors.

0 Kudos