4

for some reason I can't get row.getValue to work properly... my script will only work if I use row[0] and get the value manually but I am sure the location/order of my fields will change from dataset to dataset. This works:

    mylist = []
    with arcpy.da.UpdateCursor(FC, "*") as cursor:
        for row in cursor:
            if row[12] == value:
                mylist.append(row[2])

However this does not...

    fieldname = "Coolfield"
    mylist = []
    with arcpy.da.UpdateCursor(FC, "*") as cursor:
        for row in cursor:
            if row.getValue(fieldname) == value:
                mylist.append(row[2])

Any ideas?

11

You are observing a difference between what arcpy and arcpy.da cursors return:

The Row object represents the row of a table. The Row object is returned from InsertCursor, SearchCursor, and UpdateCursor.

and a Row object has a getValue method.

Returns an iterator of lists. The order of values in the list matches the order of fields specified by the field_names argument.

and an iterator of lists does not have a getValue method.

I don't think it is a concern that the location/order of your fields varies because you specify their order using the field_names argument.

2

If you want to do something like getValue() with arcpy.da cursors, you can do something like this:

mylist = []
with arcpy.da.SearchCursor(FC, '*') as rows:
    idx = rows.fields.index("Coolfield")
    for row in rows:
        mylist.append(row[idx)])
1

To save yourself some work trying to make arcpy.da cursors (search, update, and insert) easier to work with, I've made a cursor helper that allows bracket and dot notation with column names. So,

with arcpy.da.SearchCursor(fc, ('MyValue', 'SHAPE@')) as rows:
    for row in rows:
        my_value = row[0]
        shape = print(row[1]

Becomes:

arcpy_cursor_utility as acu

with arcpy.da.SearchCursor(fc, ('MyValue', 'SHAPE@')) as rows:
    for row in acu.search_cursor(rows):
        my_value = row.MyValue or row['MyValue']
        shape = row['SHAPE@']  # But not row.SHAPE@ because of the @ token

Note that this alteration to the out-of-the-box cursors has a performance hit, so if performance is critical, please evaluate critically.

Hope you find this useful. https://gist.github.com/BrianBunker/4a5f80ebce42082dce65

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.