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'
Solved! Go to Solution.
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).
How do you open a bug report for this kind of stuff?
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.
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).
Ha... Yeah that's totally unclear from the docs but makes sense.
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"])