rows = arcpy.UpdateCursor(infc) for row in rows: val = row.getValue (datafld) row.setValue(outfld,val) rows.updateRow(row) del rows
rows = arcpy.UpdateCursor(infc) for row in rows: if (row.getValue(inField1) == "Sandy"): row.setValue(outField, "poor") else: row.setValue(outField, "none") rows.updateRow(row) del rows arcpy.AddMessage("Completed")
Ok I have it now by NAMING the fields. Also it wouldn't work with the brackets so it works like thisrows = arcpy.UpdateCursor(infc) for row in rows: if row.getValue("Texture") == "Sandy" and row.getValue ("Humus") <= "10": #1-10 also doest not work row.setValue("Richness", "poor") else: row.setValue("Richness", "none") rows.updateRow(row) del rows, row arcpy.AddMessage("Completed") *edit - nm I got it.
if (row.getValue("Texture") == "Sandy") and (float(row.getValue("Humus")) <= 10):
if (row.getValue("Texture") == "Sandy") and (row.getValue("Humus") in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']):
Here is an updated version that uses a function (so you could call it from another script, or use it twice within the same script, etc.). Field names are hard coded here, but you could make them parameters from Arc.
To make it truly reusable you could make your conditional statements inputs to the function (as long as they followed the same format), but that is a bit more work.import arcpy def customUpdateCursor(FeatureClass, inField1, inField2, outField): '''customUpdateCursor(FeatureClass, inField1, inField2, outField) takes a feature class and three field names; two for input fields to the calculation and one for output performs a conditional assesment on the inputs and returns a value to the output ''' rows = arcpy.UpdateCursor(FeatureClass) for row in rows: # iterate over the rows if (row.getValue(inField1) == "0-5") and (row.getValue(inField2) == "Woodland"): row.setValue(outField, "15") elif (row.getValue(inField1) == "0-5") and (row.getValue(inField2) == "Forest"): row.setValue(outField, "24") else: row.setValue(outField, "0") rows.updateRow(row) del row, rows # End of customUpdateCursor function if __name__ == '__main__': featureClass = arcpy.GetParameterAsText(0) # if running from Arc as a script tool, get first (0th) parameter if len(featureClass) == 0: # if not, it comes out as '' - has length 0; use this default path featureClass = "C:\\Temp\\BAL_Mapping.gdb\\INTERFACE" # paths in python need either double slashes or forward slashed arcpy.AddMessage('Starting operation on: %s' % featureClass) infield1 = 'Slope' # you could also make these input paramaters infield2 = 'Veg' outfield = 'BALFZ' result = customUpdateCursor(featureClass, infield1, infield2, outfield)# pass variables to function arcpy.AddMessage('Completed operation on: %s' % featureClass)