field calculator python expression with parameters

122
2
03-10-2013 05:07 PM
New Contributor III
Hi, I am trying to concatonate an existing field with a parameter in a python script

parameter = 4
expression = !field! + ", " + parameter
arcpy.management.CalculateField("testshp", "field", expression, "PYTHON")

my field is a text field with a value of "1, 2, 3" and i want to make it "1, 2, 3, 4" where everytime the script runs and the user inputs
a number it adds a comma, a space, then the parameter. I keep running into issues with combining a string and an int but the parameter is preset to string. Any ideas? Thanks.
Tags (2)
Reply
0 Kudos
2 Replies
Occasional Contributor III
Maybe you should try,

expression = !field! + ", " + str(parameter)



Hi, I am trying to concatonate an existing field with a parameter in a python script

parameter = 4
expression = !field! + ", " + parameter
arcpy.management.CalculateField("testshp", "field", expression, "PYTHON")

my field is a text field with a value of "1, 2, 3" and i want to make it "1, 2, 3, 4" where everytime the script runs and the user inputs
a number it adds a comma, a space, then the parameter. I keep running into issues with combining a string and an int but the parameter is preset to string. Any ideas? Thanks.
Reply
0 Kudos
Esri Frequent Contributor
Maybe you should try,

expression = !field! + ", " + str(parameter)


I agree. James, what is the data type of your field? If you set parameter = 4, that is making it an integer. To make that a string you would need to say parameter = "4". Because you cannot concatenate strings with integers, you will need to make sure the two match. If field is a string, set parameter = "4". If the field is an integer, you will want to set parameter = 4. Otherwise, you can cast it to string or int in the expression by saying int(parameter) or str(parameter), just as suggested above.
Reply
0 Kudos