mhillig1

Help with CalculateField using variable

Discussion created by mhillig1 on Aug 31, 2013
Latest reply on Sep 1, 2013 by rfairhur24
I have been working on this on my own for some time & many frustrations, but it's time I ask for some help with my problem.

What I want to do is calculate two fields in my parcel lines data depending on the value of the 2 AOI's (polygons) on either side of
the parcel line.  For example, if the polygon on the left of a line has an AOI value of "11111" and the polygon on the right of the line
has an AOI value of "22222", the two fields in my parcel line data would be calc'd as AOI = "11111" and SharedAOI = "22222".

I have been copying portions of my script into the python window of arcmap and everything seems to work with my search cursors,
select by attributes, and select by locations.  The problem is with CalculateField.  If I hardcode a value of "11111" & "22222", the
entire process works, but if I try using a variable for those values, I get an error.  Can you please take a look and tell me what is
wrong. If I am going about this totally wrong, feel free to make suggestions.  I am fairly new to python, so any help would be
greatly appreciated.  Sorry about all the print statements and refreshes. Working on my own I keep trying to see how everything
processes.

myaoi = '69257'
fc = "AOI"

for eachaoi in aoilst:        #List of all the AOI's that surround my AOI = 69257
    sbaAOI = "AOI = '" + eachaoi + "'"
    print ("Current AOI selected = " + eachaoi)
    print sbaAOI
    arcpy.SelectLayerByAttribute_management("AOI","NEW_SELECTION",sbaAOI)
    arcpy.RefreshTOC()
    arcpy.RefreshActiveView()
    arcpy.SelectLayerByLocation_management("PLines","SHARE_A_LINE_SEGMENT_WITH","AOI")
    arcpy.RefreshTOC()
    arcpy.RefreshActiveView()
    with arcpy.da.SearchCursor(fc, "AOI") as cursor:
        for row in cursor:
            mysharedaoi = ("'" + row[0] + "'")
            print mysharedaoi
            if int(row[0]) < int(myaoi):
                print ("SharedAOI needs to be calc'd with myaoi")
                        #arcpy.CalculateField_management("PLines", "AOI",'99999')
                arcpy.CalculateField_management("PLines", "AOI", "mysharedaoi") 
                arcpy.CalculateField_management("PLines", "SharedAOI","myaoi")
                arcpy.RefreshTOC()
                arcpy.RefreshActiveView()
                print ("lines calculated")
            elif int(row[0]) > int(myaoi):
                print ("AOI needs to be calc'd with myaoi")
                arcpy.CalculateField_management("PLines", "AOI", "myaoi")
                        #arcpy.CalculateField_management("PLines", "SharedAOI",'99999')
                arcpy.CalculateField_management("PLines", "SharedAOI","mysharedaoi")
                arcpy.RefreshTOC()
                arcpy.RefreshActiveView()
                print ("lines calculated")

Outcomes