Concatenate iterating variable with string in For loop

5760
10
Jump to solution
11-23-2015 04:41 PM
EdwardGlover
New Contributor III

I summarize rows in a table in the SearchCursor and store them as variables.  I want to refer to these variables in the following update cursor to write to a table.  I get the following error on the second last line of code (in red): Tuple object does not support item assignment.  When I debug the code, ROW[0] equals 'ARSSPAN' as a string.  I want ROW[0] to reference the integer ARSSPAN summarized in the search cursor above.  In the next iteration of the UpdateCursor, I want the integer STSPAN to be written out, etc.  I feel my issue is with assembling a variable reference through concatenating a string with the iterator.  Any assistance would be greatly appreciated.

    # Create the search cursor to iterate through table and sum spans and cost

    with arcpy.da.SearchCursor(fcdtest, ("FeedClass","SHAPE_Length","MGMTCOST",)) as cursor:

        for row in cursor:

            if row[0] == "ARS":

                ARSSPAN += row[1]/55

                ARSCOST += row[2]

            elif row[0] == "ST":

                STSPAN += row[1]/55

                STCOST += row[2]

            elif row[0] == "S":

                SSPAN += row[1]/55

                SCOST += row[2]

            elif row[0] == "ARU":

                ARUSPAN += row[1]/55

                ARUCOST += row[2]

            elif row[0] == "UT":

                UTSPAN += row[1]/55

                UTCOST += row[2]

            elif row[0] == "U":

                USPAN += row[1]/55

                UCOST += row[2]

    fld = "YR2015"

    feedcls = ["ARS","ST","S","ARU","UT","U"]

    for cls in feedcls:

        with arcpy.da.UpdateCursor(out_data, (fld,)) as cursor:

            row[0] = cls + 'SPAN'

             cursor.updateRow(row)

0 Kudos
10 Replies
EdwardGlover
New Contributor III

Thank you for stating the risks of using eval and providing me with an excellent solution.  Your insight is very much appreciated.  I would like to get your opinion on the getattr function.  Could it be used as an alternative to eval?  If so, does it represent a security risk?

0 Kudos