Parameter value for Calculate Field Management?

2858
13
Jump to solution
09-30-2013 01:14 PM
ionarawilson1
Occasional Contributor III
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
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent 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...

View solution in original post

0 Kudos
13 Replies
T__WayneWhitley
Frequent Contributor
Am I wrong, or have you already gotten 'value' with your line:

value = arcpy.GetParameterAsText(1)


Can you print 'value' to see it the Get retrieved anything?
If it is good, ie the variable is populated with what you expect, then I think you can pass it into CalculateField directly as:  value
0 Kudos
ionarawilson1
Occasional Contributor III
Yes, value gets retrieved (in this case, Austin), but I still get an error.

Start Time: Mon Sep 30 16:31:56 2013
Running script CalcFeatures...
TEST
Austin

Traceback (most recent call last):
  File "D:\ArcGISData\ESRI_GEO_EX\calcfeatures_script.py", line 39, in <module>
    arcpy.CalculateField_management(Selected_Features, "EditField", value, "PYTHON_9.3", "")
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 3129, in CalculateField
    raise e
ExecuteError: ERROR 000539: Error running expression: Austin
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
NameError: name 'Austin' is not defined

Failed to execute (CalculateField).


Failed to execute (CalcFeatures).
Failed at Mon Sep 30 16:31:56 2013 (Elapsed Time: 0.00 seconds)
0 Kudos
T__WayneWhitley
Frequent Contributor
Is this related to your other post about the model where basically you want to code a field based on a Select By Location execution?  That value in the model is defined as an input parameter, meaning it is defined by you at runtime...(or, possibly, fetching the output from another tool as input in this tool).

Just a suggestion, but looks like you could also do a spatial join, but looks like you want to define the value yourself as (in the example run) 'Austin', is that right?  Well, if you are running this as a script tool, you need to set from the script interface in toolbox the input parameter...then when the interface pops up, you enter the desired val, and it gets passed during runtime as the variable 'value'... that's what GetParameterAsText means, getting what was provided during the run, if that makes sense.

Does that help?
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you Wayne, The parameter is already set in the toolbox interface. Yes, I need to have a parameter for the user to enter that value. I don't know if the problem is because I am trying to calculate a field in a layer. I tried using an updateCursor and it worked but it would be nice to know why it does not work with the calculate field function. Thank you!

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# calcfeatures_script.py
# Created on: 2013-09-30 15:22:46.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: calcfeatures_script <Selecting_Features> <value> 
# Description: 
# test
# ---------------------------------------------------------------------------

# Import arcpy module
import os, sys, arcpy, traceback, arcgisscripting

# Script arguments
Selecting_Features = arcpy.GetParameterAsText(0)
value = arcpy.GetParameterAsText(1)
arcpy.AddMessage("TEST")
arcpy.AddMessage(value)


if Selecting_Features == '#' or not Selecting_Features:
    Selecting_Features = "in_memory\\{1CFDCCDD-5D8D-42F8-AEB2-E66ED4A51A44}" # provide a default value if unspecified



# 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



with arcpy.da.UpdateCursor(Selected_Features, ("EditField")) as rows:
        # row comes back as a tuple in the order specified here, so Office is row[0], Forester is row[1]
        for row in rows:
            row[0] = value
           
            rows.updateRow(row)


0 Kudos
T__WayneWhitley
Frequent Contributor
Oh, right, your clue is here (below) - doesn't know what 'Austin' is....in other words, your CalculateField syntax is the problem - hold on and I will review the webhelp.  You are at 10.1 ?  Actually thanos hit on it at your other post...


ExecuteError: ERROR 000539: Error running expression: Austin
Traceback (most recent call last):
File "<expression>", line 1, in <module>
NameError: name 'Austin' is not defined
0 Kudos
ionarawilson1
Occasional Contributor III
Yes, 10.1 Which other post?
0 Kudos
T__WayneWhitley
Frequent Contributor
...VB is the default, so minimally you need the in_table, fieldName, and expression --- did you say you tried something like this below?

arcpy.CalculateField_management (Selected_Features, "EditField", cstr(value))


I'm not sure but is the expression param is the problem, it doesn't understand the value being passed.


EDIT:
And your Python equivalent would essentially be (sorry I think 'cstr' above is the proper conversion to string, if not already string - and as reflected below, the Python conv to string is simply 'str):

arcpy.CalculateField_management(Selected_Features, "EditField", str(value), "PYTHON_9.3")
0 Kudos
T__WayneWhitley
Frequent Contributor
I think Tim simplified things very nicely for you at your other post:
http://forums.arcgis.com/threads/93688-Model-converted-to-Script-Why-Can-t-it-find-the-field


By the way, with your use of % I think you were trying to use what is called 'in-line variable substitution', not applicable here.



Enjoy,
Wayne
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you Wayne. When I tried:

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

I get

Traceback (most recent call last):
  File "D:\ArcGISData\ESRI_GEO_EX\calcfeatures_script.py", line 32, in <module>
    arcpy.CalculateField_management(Selected_Features, "EditField", str(value), "PYTHON_9.3")
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 3129, in CalculateField
    raise e
ExecuteError: ERROR 000539: Error running expression: Austin
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
NameError: name 'Austin' is not defined

Failed to execute (CalculateField).


Failed to execute (CalcFeatures).
0 Kudos