0

I'm using ArcGIS 10.2 and trying to obtain values in a field using SearchCursor. Would someone help me with this?

import arcpy

oc = r"C:\Users\Scratch\oc.shp"

catchments = arcpy.da.SearchCursor(oc,"FEATUREID")

catchment = catchments.next()

for catchment in catchments:

   print catchment[0]

   print catchment[1] 

catchments.reset()

The catchment[0] works, but if I try anything higher, I get tuple out of range error. There are 11 rows in the shapefile, all with a unique FEATUREID. I can only extract either the 1st row or all of them at once. I need to be able to specify througout the rest of my code when I want the 1st, 2nd, 3rd, or 10th value in this shapefile under field = FEATUREID.

2
  • 2
    The line "catchment = catchments.next()" is unnecessary.
    – Barbarossa
    Commented Feb 23, 2015 at 21:08
  • 1
    And watch for syntax differences between old arcpy.SearchCursor(), and the data access arcpy.da.SearchCursor(). This can cause problems if you are not aware that there are two similar functions.
    – bebego
    Commented Feb 23, 2015 at 21:13

3 Answers 3

2

The search cursor second parameter is the field name list. You are only providing one field name that is why it is not going beyond index 0.

A list (or tuple) of field names. For a single field, you can use a string instead of a list of strings.

Try:

catchmentFields = ["FEATUREID", "field2", "field3"]
catchments = arcpy.da.SearchCursor(oc, catchmentFields)
2

Out of range means that you are trying to access a field that is not included in your result. Before you use your cursor you have to define which fields you want it to return. Those fields should be entered in a list.

In your case you would change this line:

catchments = arcpy.da.SearchCursor(oc,"FEATUREID")

to something like this:

catchments = arcpy.da.SearchCursor(oc,["FEATUREID", "anotherField","oneMoreField"])

After running your cursor, your FEATUREID would become index 0, anotherField would become index 1, etc.

So, basically, just go ahead and mention every field from your dataset that you are interested in seeing in your result.

Also, and this is important, note that the index you call corresponds to the order of the fields how you defined them when creating your cursor, it does not correspond to the order of the fields in your dataset!

1
  • Thanks, this was helpful for naming additional fields. But what if I want to designate additional rows all with the same field, FEATUREID? Commented Mar 27, 2015 at 21:20
2

You can use an asterisks to specify all of the fields in the feature class. Try the following example:

import arcpy

fc = r"C:\Users\Scratch\oc.shp"

with arcpy.da.SearchCursor(fc, "*") as cursor:
    for row in cursor:
        """
        row[0] = OID or FID
        row[1] = Centroid coordinates
        row[2] = Your 1st field
        row[3] = Your 2nd field
        etc...
        """
        print row
1
  • 1
    I think this is a much better coding pattern to follow - it is documented in the help.
    – PolyGeo
    Commented Feb 23, 2015 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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