1

I am trying to use arcpy.da.SearchCursor to get the values of two field for each reach record in the DISTRICT feature class. I need to assign these values from the table to a two variables varRegion and varDistrict for use a bit further on.

I can retrieve the "REGION" field (0) below but not the "DISTRICT" field (1).

I get the following error:

 print("REGION: {1}".format(row[1]))
 IndexError: tuple index out of range

This is the code I have written:

import arcpy

fc = 'C://Data//STATE.gdb//DISTRICT'

with arcpy.da.SearchCursor(fc,['REGION_NAME', 'DISTRICT_NAME']) as cursor:
    for row in cursor:
        varRegion = ('{0}'.format(row[0]))
        varDistrict = ('{1}'.format(row[1])) # This does not work
        print varRegion # I need the variable for a calculation further on 
        print varDistrict

I'm using ArcGIS 10.3.1

1
  • Your error message and example code don't match. Specifically, the list index being used. Is that a typo, or was that generated from other code? Jan 11, 2016 at 11:57

1 Answer 1

2

I managed to work it out

import arcpy

fc = 'C://Data//STATE.gdb//DISTRICT'

with arcpy.da.SearchCursor(fc,['REGION_NAME', 'DISTRICT_NAME']) as cursor:
    for row in cursor:
        varRegion = row[0]
        varDistrict = row[1]
        print varRegion 
        print varDistrict

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