[GIS] Very Simple ArcPy script using arcpy.SearchCursor and row.getValue

arcpycursor

I am trying to run a very simple script to test the usage of arcpy.SearchCursor and row.getValue.

I want to do a very simple exercise: I have a table with some fields, and I want to print the values of one of the fields (e.g. fldname) for every record.

The script is the below:

import arcpy
IN_Table = "C:/ZZtest/test.mdb/table"
Field1="fldname"
cursor=arcpy.SearchCursor(IN_Table)
for row in cursor:
    printf=row.getValue(Field1)
    print(printf)
del row, cursor

When I am running this I have the error below:

Traceback (most recent call last):
  File "C:\ZZtest\ScriptName.py", line 28, in <module>
    printf=str(row.getValue(Field1))
  File "C:\Program Files (x86)\ArcGIS\Engine10.2\arcpy\arcpy\arcobjects\arcobjects.py", line 1048, in getValue
    return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.

I tried to search for similar questions. But they all included more complicated scripts.

My ArcGIS version is 10.2.

The type of the "fldname" field is Integer.

Where is my mistake?

Best Answer

Welcome to the GIS SE. I have edited your script. Please note:

  1. I have updated the search cursor to include the new, faster "da" approach. Please read up on using cursors here

  2. The script below will print out the value of the fld_name for each row in the table.

  3. Please use the raw input format when trying to identify file locations.

    import arcpy
    in_table = r"c:/ZZtest/test.mdb/table"
    with arcpy.da.SearchCursor(in_table, ["fld_name"]) as scursor:
        for srow in scursor:
            print srow[0]
    del srow, scursor
    
Related Question