Alternative to Using 'getValue' with Data Access Module

1549
6
03-27-2014 10:22 AM
MikeMacRae
Occasional Contributor III
With the arcpy.da module, we cannot use the getValue method on the row object as we can in arcpy.SearchCursor:

for row in arcpy.SearchCursor(fc):
             row.getValue("SITE_ID")


We can, however, index over the columns for the row object:

with arcpy.da.SearchCursor(fc, "*") as cursor:
 for row in cursor:
  print row[1]


I want to use the data access module because it cuts my processing time by almost a half. Indexing over columns on the row object doesn't really work for me because I am using this on an excel spreasheet where users may move columns around. I prefer to pull the heading name as I can with getValue. Is there a way to do this in the da module?

Thanks,
Mike
Tags (2)
0 Kudos
6 Replies
JoshuaChisholm
Occasional Contributor III
I think you might be able to refer to it directly. For example:
with arcpy.da.SearchCursor(fc, "*") as cursor:
    for row in cursor:
        row.SITE_ID


Good luck!
0 Kudos
MikeMacRae
Occasional Contributor III
I think you might be able to refer to it directly. For example:
with arcpy.da.SearchCursor(fc, "*") as cursor:
    for row in cursor:
        row.SITE_ID


Good luck!


Thanks Jochua, but I've tested that as well and I am retunring the same error:

AttributeError: 'tuple' object has no attribute SITE_ID'


There must we a way to do this without having to index like this:

with arcpy.da.SearchCursor(fc, "*") as cursor:
    for row in cursor:
        row[2]
0 Kudos
JoshuaChisholm
Occasional Contributor III
Opps, I'm sorry Mike. That was a function of arcpy.SearchCursor (not arcpy.da.SearchCursor).

You can try something like this:
cur=arcpy.da.SearchCursor(fc, "*")
c=0 #counter
d={} #dictionary used to call the index for a given field name
for field in cur.fields:
    d[field]=c
    c+=1

for row in cur:
    print row[d['SITE_ID']]


Let me know if that works!
0 Kudos
by Anonymous User
Not applicable
I think it would be easiest to get the index by the name using the built in list index property:

with arcpy.da.SearchCursor(fc, '*') as cur:
    for row in rows:
        print row[cur.fields.index('SITE_ID')]


It would actually more efficient to not have the index in the loop:

with arcpy.da.SearchCursor(fc, '*') as cur:
    ind = cur.fields.index('SITE_ID')
    for row in rows:
        print row[ind]


This way it does not have to find the index inside each iteration since the index will not change.
0 Kudos
JasonScheirer
Occasional Contributor III
We did a Blog post on this some time ago. Are you only accessing a single column? In your example, that appears to be the case. If you know you're just grabbing a certain set of column values, you can specify them when you open the cursor and then you know what index they'll be at. So you'd be best off doing this:
with arcpy.da.SearchCursor(fc, 'SITE_ID') as cur:
    for row in rows:
        print row[0]


or
with arcpy.da.SearchCursor(fc, ['SITE_ID', 'OTHER_FIELD']) as cur:
    for row in rows:
        print row[0], row[1]


etc.
0 Kudos
T__WayneWhitley
Frequent Contributor
ah, thank you Jason, didn't see that before...wow, and that's a slick one-liner in the def function:
yield dict(zip(colnames, row))


Lots to read and learn...and relearn.

Wayne
0 Kudos