I am receiving the error that variable row is not defined (second line of codeblock). I am attempting to check "constraints_Merge_fl" against code value in "lepc_clip_fl". What does the correct usage of this variable in the codeblock look like?
fc = "lepc_clip"
code = "CODE"
field = "OBJECTID"
# Make fl's for select tools
arcpy.MakeFeatureLayer_management(fc,fc + "_fl")
arcpy.MakeFeatureLayer_management("constraints_Merge","constraints_Merge_fl")
cursor = arcpy.SearchCursor(fc + "_fl")
row = cursor.next()
arcpy.AddField_management("constraints_Merge_fl","HEX_ID","Long")
chat_List = []
while row:
code_Value = row.getValue(code)
hex_ID = row.getValue("Hex_ID")
chat_List.append(hex_ID)
for feature in chat_List:
arcpy.SelectLayerByAttribute_management (fc + "_fl","NEW_SELECTION",'"Hex_ID" = {}' .format(feature))
arcpy.SelectLayerByLocation_management("constraints_Merge_fl",'intersect', fc + "_fl")
expression = "getClass(int(!CODE!))"
# Check syntax for row.getValue(code) in codeblock
codeblock = """def getClass(code):
if code < row.getValue(code):
return row.getValue(code)
else:
return code"""
arcpy.CalculateField_management("constraints_Merge_fl", "CODE", expression, "PYTHON_9.3", codeblock)
row = cursor.next()
Thanks in advance.
Hi Noah,
I would recommend using the UpdateCursor to update the field. In my opinion, it's a little easier to code. Also, if you are using 10.1 or above, you can use the arcpy.da.UpdateCursor that is even faster. Below is an example using the UpdateCursor with a data dictionary rather than a list:
fc = "lepc_clip"
code = "CODE"
field = "OBJECTID"
arcpy.MakeFeatureLayer_management(fc,fc + "_fl")
arcpy.MakeFeatureLayer_management("constraints_Merge","constraints_Merge_fl")
chat_List = {}
cursor = arcpy.SearchCursor(fc + "_fl")
for row in cursor:
code_Value = row.getValue(code)
hex_ID = row.getValue("Hex_ID")
chat_List[hex_ID] = code_Value
del row, cursor
for feature in chat_List:
expression = "Hex_ID = " + str(feature)
arcpy.SelectLayerByAttribute_management (fc + "_fl", "NEW_SELECTION", expression)
arcpy.SelectLayerByLocation_management("constraints_Merge_fl", 'intersect', fc + "_fl")
cursor = arcpy.UpdateCursor("constraints_Merge_fl")
for row in cursor:
if row.Code < chat_List[feature]:
row.Code = chat_List[feature]
cursor.updateRow(row)
del row, cursor