I am trying to write a script where you preemptively select features and then go to the tool which calls for the feature layer and it populates fields based on certain criteria which I will design of the selected features.
I am simply trying to populate fields right now using the Update Cursor, but the field still come up blank.
here is my code so far:
input= arcpy.GetParameterAsText(0)
fields= ['Conf','Com']
with arcpy.da.UpdateCursor(input,fields) as cursor:
for row in cursor:
row[0] == '2'
cursor.updateRow(row)
del row
del cursor
Why are the selected features, or any feature for that matter not updated?
-
5Do not del row. row[0]='2'. Also do not use word input, do not del cursorFelixIP– FelixIP2016年01月18日 00:45:21 +00:00Commented Jan 18, 2016 at 0:45
1 Answer 1
To expand more on @FelixIP comment, your row[0] is not assigning a value since double "==" are used with a if conditional statement for evaluating a value to be true or false. Use single "=" for committing a value change instead. Also, since your cursor is used within a with loop you do not need to delete the cursor and row objects.
Try this instead for your example:
fc = arcpy.GetParameterAsText(0)
fields = ['Conf','Com']
with arcpy.da.UpdateCursor(fc,fields) as cursor:
for row in cursor:
row[0] = '2'
cursor.updateRow(row)
To update only selected features you will have to make the input fc a feature layer first (use MakeFeatureLayer method) and then perform a Select Layer By Attribute or Select Layer By Location method before entering the cursor.