TypeError: 'Row' object is not subscriptable

217
2
Jump to solution
08-12-2024 11:43 AM
rescobar
Occasional Contributor

Hello, 

 

I'm have the following code trying to compare a field of different feature classes:

import arcpy
input_table = arcpy.GetParameterAsText(0)
input_fld = arcpy.GetParameterAsText(1)
compare_table = arcpy.GetParameterAsText(2)
compare_fld = arcpy.GetParameterAsText(3)
arcpy.AddMessage(input_fld)
#create a search cursor for the compare table
compare_cursor = arcpy.SearchCursor(dataset=compare_table, fields=compare_fld)
#create a set of all values in the compare cursor
compare_set = set()
for row in compare_cursor:
    arcpy.AddMessage(row[0])
    compare_set.add(row[0])
arcpy.AddMessage(compare_set)
#add a "compare field" if this is not already present 
input_fields = arcpy.ListFields(input_table)
if not "Compare" in input_fields:
    arcpy.management.AddField(input_table, "Compare", "TEXT")
#log if the value in the input fields exist in the compare table, by marking "yes" or "no" in the "compare" field
update_curs = arcpy.UpdateCursor(input_table, [input_fld, "Compare"])
for row in update_curs:
    if row in compare_set:
        row[0] = "YES"
    else:
        row[0] = "NO"
    update_curs.updateRow(row)

 

This give me the following error, shouldn't a row in a search cursor be subscriptable by default?

field_compare
=====================
Parameters

Input Table E:\Users\58303\Documents\ArcGIS\Projects\test.gdb\Input_table
Input Field Input_field
Compare Table E:\Users\58303\Documents\ArcGIS\Projects\test.gdb\Compare_table
Compare Field Compare_Field
=====================
Messages

Start Time: Monday, August 12, 2024 2:39:21 PM
Input_field
Traceback (most recent call last):
File "E:\Users\58303\Documents\ArcGIS\Projects\Experimental.atbx#fieldcompare_NewToolbox.py", line 13, in <module>
TypeError: 'Row' object is not subscriptable

Failed script field_compare...
Failed to execute (fieldcompare).
Failed at Monday, August 12, 2024 2:39:21 PM (Elapsed Time: 0.43 seconds)

 

 

 

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

You are mixing the two types of cursors in ArcPy; specifically, you are using the old-style cursor and trying to use new-style syntax.  See Data access using cursors—ArcGIS Pro | Documentation

View solution in original post

2 Replies
JoshuaBixby
MVP Esteemed Contributor

You are mixing the two types of cursors in ArcPy; specifically, you are using the old-style cursor and trying to use new-style syntax.  See Data access using cursors—ArcGIS Pro | Documentation

DanPatterson
MVP Esteemed Contributor

SearchCursor—ArcGIS Pro | Documentation

see example 2 for da.SearchCursor and a single field


... sort of retired...
0 Kudos