My code has to check if a value in a shapefile attribute table belongs to a certain group of landuses. Below I paste my code:

cursor = arcpy.SearchCursor(buf_intersect)
fields = ["GTYS_Branc", "landuse", "landuse_1", "landuse_12", "landuse_13", "Shape_Area"]

for row in cursor:
    book = xlrd.open_workbook("MyDirectory.xls")
    first_sheet = book.sheet_by_index(0)
    index = row.getValue(fields[0])

    # Rural

    if (row.getValue(1) == 'field') or (row.getValue(1) == 'farmland') or (row.getValue(1) == 'grass') or (row.getValue(1) == 'quarry') or (row.getValue(1) == 'paddock') or (row.getValue(1) == 'vineyard') or (row.getValue(1) == 'reservoir') or (row.getValue(1) == 'orchard') or (row.getValue(1) == 'greenfield') or (row.getValue(1) == 'allotment') or (row.getValue(1) == 'conservation') or (row.getValue(1) == 'moorland') or (row.getValue(1) == 'f'):
        col = 2

    # Suburban

    elif (condition similar to the one above)
        col = 3

And so on. When I run this code, I receive the following error:

Traceback (most recent call last):
  File "Mydirectory.py", line 32, in <module> the first if line
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\arcobjects.py", line 1100, in getValue
    return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.

What can the reason be? May it be because I have too many conditions on my if statement?


It looks like you've mixed up the usage of the 'old' cursors (arcpy.SearchCursor) and 'new' cursors (arcpy.da.SearchCursor).

You are using the 'old' cursors, so you need to pass the name of the field to getValue, not the field index.

So in your case, change row.getValue(1) to row.getValue(fields[1]).

Also, there's no need to have all of the or statements if you are just checking a single field. You can do something like:

if row.getValue(fields[1]) in ('field','farmland','grass',
  • Thank you, that was actually the problem and now the code does not record the error on this line anymore. Also thank you for the piece of advice to make the core more elegant avoiding all those "or" – Patapunfate Apr 20 '16 at 12:03

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.