Calculate fields using logic with Python - standalone script

3760
13
Jump to solution
04-22-2021 05:01 AM
KK2
by
New Contributor III

Hallo, I would like to ask how to populate a new attribute field using logic with python as standalone script without opening Raster Calculator and pasting the script attached below. How to assign with python the information from which attribute(s) and from which dataset the new attribute will be populated?

Expression:
Reclass(!WELL_YIELD!)

Code Block:
def Reclass(WellYield):
    if (WellYield >= 0 and WellYield <= 10):
        return 1
    elif (WellYield > 10 and WellYield <= 20):
        return 2
    elif (WellYield > 20 and WellYield <= 30):
        return 3
    elif (WellYield > 30):
        return 4

 

0 Kudos
13 Replies
KK2
by
New Contributor III

Below I attach the updated script and the error message. I also changed 'float' to 'short' in expression line, because my attribute is saved as short, but it did not change anything.

 

# Import system modules
import arcpy
# Set environment settings
arcpy.env.workspace = "E:/CLC_data"

# Set local variables
inFeatures = "CLC"
fieldName = "NEW_code"
#fieldPrecision = 9
#fieldAlias = "refcode"
fieldLength = 50
 
# Execute AddField for new fields
arcpy.AddField_management(inFeatures, fieldName, "TEXT", field_length=fieldLength)

# Set local variables
inTable = "CLC"
expression = "getClass(short(!CLC1!))"

#Create NEW_code for CLC1
codeblock = """
def getClass(CLC1):  
  if (CLC1 == 1):  
    return 'A_1'  
  elif (CLC1 == 2):  
    return 'A_2'
  elif (CLC1 == 3):  
    return 'A_3'   	
  else:
	return 'error'"""
 	
# Execute CalculateField 
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3", 
                                codeblock)

 

 

 

Runtime error  Traceback (most recent call last):   File "<string>", line 3, in <module>   File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 3183, in CalculateField     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000800: The value is not a member of VB | PYTHON | PYTHON_9.3. Failed to execute (CalculateField).  
>>> 

 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

Calculate Field (Data Management)—ArcGIS Pro | Documentation

arcpy.management.CalculateField(in_table, field, expression, {expression_type}, {code_block}, {field_type})

arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3",
codeblock)

"getClass(short(!CLC1!))"  # wrong

leave out short

"getClass(!CLC1!)"

AND the error says....

    .... not a member of VB | PYTHON | PYTHON_9.3

and you put .... "PYTHON3",

try  "PYTHON" or  "PYTHON_9.3"


... sort of retired...
KK2
by
New Contributor III

Thank you again for your valuable help. Amending your suggestions helps me to run the script successfully. I used PYTHON3 before, because such example I have found on ESRI website.

With these changes the script works:

expression = "getClass(!CLC1!)"
# Execute CalculateField 
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON", 
                                codeblock)

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

@KK2 Always use the current help files to validate syntax.  You would not be the first one that gets led astray by "googling" for help.

I always navigate from here

ArcGIS Pro help—ArcGIS Pro | Documentation for general help

ArcGIS Pro geoprocessing tool reference—ArcGIS Pro | Documentation for tool information and

ArcGIS Pro Python reference—ArcGIS Pro | Documentation for python specific information.

Take care 


... sort of retired...