Select to view content in your preferred language

Parameter value for Calculate Field Management?

3956
13
Jump to solution
09-30-2013 01:14 PM
ionarawilson1
Deactivated User
Can anybody tell me how I can grab the value of the second parameter (value) to use in my Calculate Field Management process? Right now is just adding %value% to the table instead of the value I enter as a parameter. Thank you!


# Import arcpy module import arcpy  # Script arguments Selecting_Features = arcpy.GetParameterAsText(0) if Selecting_Features == '#' or not Selecting_Features:     Selecting_Features = "in_memory\\{1CFDCCDD-5D8D-42F8-AEB2-E66ED4A51A44}" # provide a default value if unspecified  value = arcpy.GetParameterAsText(1)  # Local variables: Input_Points = "D:\\ArcGISData\\ESRI_GEO_EX\\GDB.gdb\\SamplePoints" Final_Output = value  Input_Points_Layer = "SamplePoints_Layer"  # Process: Make Feature Layer arcpy.MakeFeatureLayer_management(Input_Points, Input_Points_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;SymbologyField SymbologyField VISIBLE NONE;DomainField DomainField VISIBLE NONE;Shape Shape VISIBLE NONE;Subtype Subtype VISIBLE NONE;EditField EditField VISIBLE NONE")  # Process: Select Layer By Location arcpy.SelectLayerByLocation_management(Input_Points_Layer, "INTERSECT", Selecting_Features, "", "NEW_SELECTION") Selected_Features = Input_Points_Layer # Process: Calculate Field arcpy.CalculateField_management(Selected_Features, "EditField", "'%value%'", "PYTHON_9.3", "")
Tags (2)
0 Kudos
13 Replies
T__WayneWhitley
Honored Contributor
...oh, sorry, I don't usually use the Calculate Field tool but opt for using cursor...so the hint was the expression wasn't valid.

You need to test this but I think it's looking for this (below) as the expression, I'm not sure but worth a try:

arcpy.CalculateField_management(Selected_Features, "EditField", '"' + value + '"', "PYTHON_9.3") 


Let me know if that works...
0 Kudos
ionarawilson1
Deactivated User
Yes!!! Thank you again! This is awesome! It was driving me crazy for two days!
0 Kudos
T__WayneWhitley
Honored Contributor
Yes, well, this is just an additional note -- maybe it'll interest you and maybe not, but I think it could be instrumental for you to check out some of the samples from the webhelp pertaining to the expression parameter and further, the code block parameter.

IMHO it is a confusing way to write 'code' but certainly interesting (if not somewhat bewildering) to have this option which I am quoting an excerpt of from the webhelp link I'll also include below (notice how the quoting was used):
# Set local variables
inTable = "parcels"
fieldName = "areaclass"
expression = "getClass(float(!SHAPE.area!))"
codeblock = """def getClass(area):
    if area <= 1000:
        return 1
    if area > 1000 and area <= 10000:
        return 2
    else:
        return 3"""
 
# Execute CalculateField 
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON_9.3", codeblock)


Calculate Field (Data Management)
Desktop » Geoprocessing » Tool reference » Data Management toolbox
http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004m000000


...for what it's worth!

Enjoy,
Wayne
0 Kudos
ionarawilson1
Deactivated User
Very interesting indeed. Thank you for the link and the explanation. It is very helpful! Cheers
0 Kudos