I have a series of fields to check for a certain value, then calculate other fields with using UpdateCursor.

Fields are: SPECIES_CD_1, SPECIES_CD_2, etc.. up to SPECIES_CD_99

Here is the general idea (some pseudocode in here):

rows = arcpy.UpdateCursor(FC) 
speciesLevel = "SPECIES_CD_"
for x in range (2,99):
    speciesX = speciesLevel + str(x) #this is a string
    speciesUnicode = speciesX.decode #Decodes the speciesX string to Unicode

    for row in rows:
    if row.speciesUnicode == 'P%':
        [do a whole bunch of things]
    elif row.speciesUnicode == None: 
        [do other things]
        [do other things]

Of course the cursor cannot identify a row named speciesUnicode, so the cursor fails to run. I want speciesUnicode to act as a variable that subs in a field name. Is this possible somehow?

  • 1
    There is no UpdateCursor in the code presented. For this to qualify as a code snippet that works up to where you are stuck you need to show just a little more code. As it stands this looks more like a Python (Stack Overflow) than ArcPy question. – PolyGeo Aug 29 '17 at 23:37
  • 2
    Pseudo-code doesn't cut it when the actual Python is required to tell what you're doing wrong. – Vince Aug 29 '17 at 23:37
  • 1
    Which actual cursor are you trying to use? There is 2 different types: arcpy.UpdateCursor and arcpy.da.UpdateCursor, they are fundamentally different, which is what @PolyGeo is saying, we need to see the real code where you declare the cursor and try to access the values. – Michael Stimson Aug 29 '17 at 23:44
  • Thanks all. I have added some lines to clarify I am using arcpy.UpdateCursor. I am open to changing to arcpy.da.UpdateCursor if that works better for this purpose. – grego Aug 29 '17 at 23:50
  • 2
    You shouldn't really use an arcpy.UpdateCursor for anything. – Vince Aug 29 '17 at 23:59

You seem to be looking for values in all your SPECIES_CD_ fields that start with P. Here's a way to do this using list indices applied to your row. Note that I'm using the data access cursor so hopefully you're at 10.1 or above.

fc = ... #feature class
flds = ... #fields for cursor
with arcpy.da.UpdateCursor (fc, flds) as curs:
    for row in curs:
        for x in range (2, 99):
            fldName = "SPECIES_CD_" + str (x) #field name
            fldIndex = flds.index (fldName) #field index
            fldVal = row [fldIndex] #field value
            if type (fldVal) == str: ### check if value is a string
                if fldVal.startswith ("P"): #check if value starts with 'P'
                    [do a whole bunch of things]
            if fldVal == None:
                [do other things]
                [do other things]

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.