1

Does the arcpy.SearchCursor function support field indexing? In the sample script below, can I sent my cursor to loop through the values in the first field by setting an index to search the first field?

for x in arcpy.SearchCursor(filepath):
    print x[0]
1
  • Could you explain why you might want to do this?
    – blah238
    Commented Aug 30, 2013 at 2:05

2 Answers 2

2

My quick test below shows that the answer to your question is "No".

>>> for x in arcpy.SearchCursor(r"C:\polygeo\ArcTutor\Editing\Zion.gdb\Roads"):
...     print x[0]
...     
Runtime error 
Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: 'Row' object does not support indexing

However, if you are using ArcGIS for Desktop 10.1 or 10.2 then arcpy.da.SearchCursor provides the functionality that you seek.

>>> for x in arcpy.da.SearchCursor(r"C:\polygeo\ArcTutor\Editing\Zion.gdb\Roads","*"):
...     print x[0]
...     
172
173
174
175
1

It is not entirely clear why you want to do this, or which version of ArcGIS you are using, but you could do something like this at 10.0 if you are not at 10.1+:

fc = "some_feature_class.shp"
fieldnames = [field.name for field in arcpy.ListFields(fc)]
for row in arcpy.SearchCursor(fc):
    print row.getValue(fieldnames[0])

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.