0

I want to to is have a user select a feature class, which then populates the fields that can be selected (this works), create a new field in that feature (works), and then populate the new field with values based on the inputField values. Right now, it just populates the new field with the field name of the inputField.

It seems like this question has been asked a thousand times, but I still can't get this to work.

import os
import arcpy
from arcpy import env
env.overwriteOutput = 1  

inputFC = arcpy.GetParameterAsText(0)
inputField = arcpy.GetParameterAsText(1)
inputFieldLabel = "GeoLabel"
output = arcpy.AddFieldDelimiters(inputFC, inputField)
inputFieldName = arcpy.GetParameterAsText(2)

#Add the GeoLabel field to the Selected Feature Class
arcpy.AddField_management(inputFC, inputFieldLabel, "TEXT")

#Calculate the field values for the selected field, using UniueID field and InputFieldName
arcpy.CalculateField_management(inputFC, inputFieldLabel, output, "Python_9.3")
3

The help article for the AddField_management function states that it "Returns a delimited field name." See the example code in the document for proper usage.

Essentially what you are specifying as your SQL expression is a string which, depending on the data source, will be either "GeoLabel" or [GeoLabel]. What I think you are trying to do here is get the value of the field each record and assign it to the new field, which you can do by creating a cursor object, at which point you might as well use an update cursor to update the new field instead of the calculate field tool. Your code would look something like this:

import os
import arcpy
from arcpy import env
env.overwriteOutput = 1  

inputFC = arcpy.GetParameterAsText(0)
inputField = arcpy.GetParameterAsText(1)
inputFieldLabel = "GeoLabel"
output = arcpy.AddFieldDelimiters(inputFC, inputField)
inputFieldName = arcpy.GetParameterAsText(2)

#Add the GeoLabel field to the Selected Feature Class
arcpy.AddField_management(inputFC, inputFieldLabel, "TEXT")

#Get value of the inputFieldLabel field and update the new field with that value
with arcpy.da.UpdateCursor(inputFC,inputField,inputFieldLabel) as cur:
    for row in cur:
        row[2] = row[1]
        cur.updateRow(row)

#Calculate the field values for the selected field, using UniueID field and InputFieldName
#arcpy.CalculateField_management(inputFC, inputFieldLabel, output, "Python_9.3")

@user2856159's answer is also good, I'd be curious to see if one method is any quicker than the other - da.UpdateCursor() once vs. multiple CalculateField_management calls.

1

My understanding from reading the AddFieldDelimiters documentation is that it's not necessary to use it for field calculator, it's more for SQL statements (ex: definition query or select by attribute): http://help.arcgis.com/en%20/arcgisdesktop/10.0/help/index.html#//000v0000004n000000

Instead, I would, as the CalculateField documentation's examples show, just wrap the field names in "!" and see if that works instead. http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004m000000

So maybe try replacing the current output = ... line with

output = "!" + inputField + "!"

See if that works, I don't make any promises that it for sure will as I don't know if there might be other issues I'm not noticing, but that's what it's looking like the problem is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.