SearchCursor row methods missing?

812
5
Jump to solution
09-14-2022 06:59 AM
mmann1123
New Contributor II

Using arcpy 3.0 - all the row methods described here appear to be missing for a feature layer in a file geodatabase??

For instance the only printed methods for the following

 

with arcpy.da.SearchCursor(in_table="Crashes_in_DC",
field_names="Tot_PedBik",) as cursor:
    for row in cursor:
         print(dir(row))

 

 are

 

'count', 'index'

 

 

So

 

with arcpy.da.SearchCursor(in_table="Crashes_in_DC",
                          field_names="Tot_PedBik",) as cursor:
    for row in cursor:
        print(row.getValue('Tot_PedBik'))

 

returns:

 

AttributeError: 'tuple' object has no attribute 'getValue'

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

That documentation is referring to the legacy cursor, but your code sample is using the new "data access" cursor. I'm surprised the legacy cursor documentation isn't more clearly identified here. The new "da" cursors returns the row data as a Python tuple (instead of a row object), which only has the methods built in for a tuple. To access data in a particular field, use the index of the field.

with arcpy.da.SearchCursor(in_table="Crashes_in_DC",
                          field_names=["Tot_PedBik"]) as cursor:
    for row in cursor:
        print(row[0])

Here, it's using index 0 because that's the index of Tot_PedBik in the field_names parameter (which should be a list, even with one field).

View solution in original post

5 Replies
mmann1123
New Contributor II

How do you open a bug report for this kind of stuff? 

0 Kudos
BlakeTerhune
MVP Regular Contributor

There's a feedback option at the bottom of all documentation pages. I already submitted my feedback on this, but you're welcome to submit your feedback too.

0 Kudos
BlakeTerhune
MVP Regular Contributor

That documentation is referring to the legacy cursor, but your code sample is using the new "data access" cursor. I'm surprised the legacy cursor documentation isn't more clearly identified here. The new "da" cursors returns the row data as a Python tuple (instead of a row object), which only has the methods built in for a tuple. To access data in a particular field, use the index of the field.

with arcpy.da.SearchCursor(in_table="Crashes_in_DC",
                          field_names=["Tot_PedBik"]) as cursor:
    for row in cursor:
        print(row[0])

Here, it's using index 0 because that's the index of Tot_PedBik in the field_names parameter (which should be a list, even with one field).

mmann1123
New Contributor II

Ha... Yeah that's totally unclear from the docs but makes sense. 

0 Kudos
Luke_Pinner
MVP Regular Contributor

If you want to use field names to refer to row values instead of tuple indexes, you can convert the row to a dict:

 

 

with arcpy.da.SearchCursor(feature_class, ["list", "of", "field", "names"]) as rows:
    fields = rows.fields
    for row in rows:
        row = dict(zip(fields, row))
        print(row['SOMEFIELD'], row["ANOTHERFIELD"])