[GIS] Using ArcPy to set field values for all features within each polygon of separate feature class

arcgis-10.1arcgis-desktoparcpyerror-999999

I am trying to set a field value in one feature class (TRU) based on the value of a field in a separate feature class (DerivedBoundaries) that contains the first, using python in ArcGIS 10.1.

I have written a script that iterates through each site boundary, selects all of the TRU features inside of it, and does that pretty well. However, I cannot seem to get my head around how the arcpy.CalculateField_management() function operates.

#Build an array and fill it with all the TempIDs numbers present in the data table
rows = arcpy.SearchCursor(lyr_DerivedBoundaries)
row = rows.next()

TRU_rows = arcpy.UpdateCursor(lyr_TRU)
TRU_row = rows.next()

while row:
    a = row.TempID
    TempIDs.append(a)
    row = rows.next()

    #Set Count equal to the length of the array created in the while loop
    Count = len(TempIDs)

while Position < Count: #iterate through each individual feature by using a definition query
    query = "[TempID] = " + "'" + TempIDs[Position] + "'"
    lyr_DerivedBoundaries.definitionQuery = query

    #center on feature
    df.panToExtent(lyr_DerivedBoundaries.getSelectedExtent())

    #Select all of the TRU Cells within the active Site Boundary
    arcpy.SelectLayerByLocation_management(lyr_TRU,"COMPLETELY_WITHIN",lyr_DerivedBoundaries,0,"NEW_SELECTION")

    #Set [Assoc_Site] equal to lyr_DerivedBoundaries.[TempID]
    hopper = '""""' + str(TempIDs[Position]) + '""""'
    arcpy.CalculateField_management("TRU","Assoc_Site",hopper,"VB","#")

I have been attempting to make the third argument in the CalculateField function operate based on the syntax I am seeing from copying a successful CalculateField operation as a python snippet from the results window in ArcMap.

Any help is greatly appreciated, I'm open to the possibility that I am going about this is a roundabout way and that there is a better way.

Thanks!

Edit: Just in case, here is the text of the error message I get when the script hits the line with the CalculateField function on it:

Runtime error Traceback (most recent call last): File "", line 43, in File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 3129, in CalculateField raise e ExecuteError: ERROR 999999: Error executing function. Expected end of statement Failed to execute (CalculateField).

Best Answer

Fixed! Amazing what a few minutes away from the code will do...

The problem was that I was including too many quotation marks when I set the value for 'hopper'. I changed:

hopper = '""""' + str(TempIDs[Position]) + '""""'

To:

hopper = '"' + str(TempIDs[Position]) + '"'

And now it runs just like I want it to!

Related Question