Select to view content in your preferred language

Add/Calculate Field within a python script

6203
7
Jump to solution
03-14-2013 11:52 AM
AlexGole1
Emerging Contributor
Hi all,
I am trying to build a tool in python that selects by location, copy to new feature class, add a field to the new feature class, and calculate the field that was just added in the script. The first part of the script works well, however it stops working when adding field/calculating field. I am not quite sure why...
The error is: ERROR 000728 on CalculateField: Field Does not exist. In my script I specifically added a python line to add a line.
What lines in this script are wrong (adding field or calculate field, both?)
Thanks,
Alex

PS: The calculate field first purpose was to do the following: the field "Update" = "arcpy.GetParameter(3)" (the text will be entered by the tool user.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Alum
Thank you! It seems like the script is running better now. However, I still come back to my first error. ERROR 00728: Field Update does not exist within table... I am not sure why because the two lines before calculating the field were specifically written to create this field...


My guess is the field name "Update" was not legal for that workspace/table type, so the field name got saved as something legal like  "UPDATE_". (UPDATE is a reserved word in SQL.)

If I try to add a field to a fGDB table with this name interactively, I get a warning:

WARNING 000304: Renamed field Update to Update_ Adding Update_ to sampcells_RotateFeatures...


See the help for arcpy.ValidateFieldName for more details on how to deal with that.

View solution in original post

0 Kudos
7 Replies
LucasDanzinger
Esri Frequent Contributor
I think the issue is with your Expression variable. Here is what you have:

UpdatedField = "Update"
Expression = (" = " + "'" + arcpy.GetParameter(3) + "'")
arcpy.CalculateField_management(Selected_cnddb_point, UpdatedField, Expression, "PYTHON")


If your user entered parameter was "hello", the expression would be: ='hello' and not: "Update" = 'hello'.

I think your Expression variable should actually be:

Expression = '"Update" = ' + "'" + arcpy.GetParameter(3) + "'"
0 Kudos
AlexGole1
Emerging Contributor
Thanks for your quick answer. I just tried your line. The positive thing is that i get a different error. The error is: "cannot concatenate 'str' and 'geoprocessing value object' objects".
I am not sure how else I could possibly calculate this field...
0 Kudos
curtvprice
MVP Alum
I think your Expression variable should actually be:

Expression = '"Update" = ' + "'" + arcpy.GetParameter(3) + "'"


Or better yet, use Python string formatting - easier to get the syntax right. The expression just has to be the part that you would have typed into the Calculate Field dialog -- "Update = " is handled by putting your field UpdatedField in the second argument.

The error is: "cannot concatenate 'str' and 'geoprocessing value object' objects".


I also recommend using arcpy.GetParameterAsText unless you have a special reason for needing to use GetParameter (value tables etc).  The text representation is usually much easier to work with, as  you have discovered.

I moved the message first because it makes sense to say what you're about to do before you do it...

# Add Field
arcpy.AddMessage("Adding Updated Field...") 
arcpy.AddField_management(Selected_cnddb_point, UpdatedField, "TEXT", "", "", "50","", "NULLABLE")


#Calculate Field
arcpy.AddMessage("Calculating Updated Field...")
Expression = "'{0}'".format(arcpy.GetParameterAsText(3)) # single quoted string literal
arcpy.CalculateField_management(Selected_cnddb_point, UpdatedField, Expression, "PYTHON")
0 Kudos
AlexGole1
Emerging Contributor
Thank you! It seems like the script is running better now. However, I still come back to my first error. ERROR 00728: Field Update does not exist within table... I am not sure why because the two lines before calculating the field were specifically written to create this field...
0 Kudos
curtvprice
MVP Alum
Thank you! It seems like the script is running better now. However, I still come back to my first error. ERROR 00728: Field Update does not exist within table... I am not sure why because the two lines before calculating the field were specifically written to create this field...


My guess is the field name "Update" was not legal for that workspace/table type, so the field name got saved as something legal like  "UPDATE_". (UPDATE is a reserved word in SQL.)

If I try to add a field to a fGDB table with this name interactively, I get a warning:

WARNING 000304: Renamed field Update to Update_ Adding Update_ to sampcells_RotateFeatures...


See the help for arcpy.ValidateFieldName for more details on how to deal with that.
0 Kudos
AlexGole1
Emerging Contributor

Thanks!!! It works like a charm now!
Just for future reference, can you point me the ESRI web help for the field calculator used in python script.
For instance, I never saw this in any help document before:

"'{0}'".format(arcpy.GetParameterAsText(3))

Thanks again

0 Kudos
curtvprice
MVP Alum
You're welcome!

Can you point me the ESRI web help for the field calculator used in python script.
For instance, I never saw this in any help document before:

Expression = "'{0}'".format(arcpy.GetParameterAsText(3))


This is a Python thing. Although there are three main ways to do string formatting in Python, the format() function is current way to do it.

Python Tutorial: 7.1. Fancier Output Formatting
0 Kudos