Calculate Field management throwing field name error

452
2
Jump to solution
04-10-2013 11:05 AM
StephenKruzik
New Contributor III
I have been racking my brain for over a week now and no matter how I format my code, the arcpy.CalculateField_management line just will not run for me. 

The parts of the code for the operation are below.

arcpy.env.overwriteOutput = True env.workspce = "F:/GIS Data/Drivetesting.gdb" #mxd = arcpy.mapping.MapDocument("CURRENT") --used in the Arcpy environment #df = arcpy.mapping.ListDataFrames(mxd, "New Data Frame")  --used in the Arcpy environment Field = "PCode1" FC = "F:/GIS Data/Drivetesting.gdb/Carrier/Phone" MyField = Myfield = arcpy.ListFields(FC)[24] arcpy.MakeFeatureLayer_management(FC,"FC_lyr") ---------> tons of geoprocessing going on right here, but not included as not relevant to question (and saves space!)     mylist = []     # Iterate through the rows in the cursor       for item in Scursor:         a = (item.Frequency, item.PCode1,)         mylist.append(a)              mylist.sort()     mylist.reverse()     del mylist[1:]     applist = [x[1] for x in mylist]     applist.append(CellIDs.UNIQUE_ID)     applist.append(CellIDs.CellID)     print applist     PCode1 = str(applist[0])     PCode2 = str(applist[1])     PCode3 = str(applist[2])     Unitname = str(applist[3])     Columnname = '"Cellname1"'     print Cellname     print MyField.name          arcpy.SelectLayerByAttribute_management("FC_lyr","SUBSET_SELECTION",' "PN1" = ' + PCode1 + " or \"PN1\" = " + PCode2 + " or \"PN1\" = " + PCode3)     arcpy.CalculateField_management("FC_lyr",Columnname,Unitname)


It throws

ERROR 000728: Field "Cellname1" does not exist within table Failed to execute (CalculateField).


I've tried writing out Cellname1, I've tried getting it through arcpy.listfields.  I've tried single quotes, double quotes, quoting the quotes...you name it; I think I've tried it.  If anyone has any idea why this is happening, I would greatly appreciate some assistance as I am at my wits end here.  I know the field name exists as the 'print MyField.name' is derived from the listfields function, and gives me "Cellname1" as my result. PLEASE HELP!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Stephen,

Syntax for the Calculate Field tool can be tricky.  Change your two variables to the following:

Columnname= "Cellname1" Unitname = '"' + str(applist[3]) + '"'


The expression needs to have quotes at the start and end, even if the variable is a string.

View solution in original post

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Stephen,

Syntax for the Calculate Field tool can be tricky.  Change your two variables to the following:

Columnname= "Cellname1" Unitname = '"' + str(applist[3]) + '"'


The expression needs to have quotes at the start and end, even if the variable is a string.
0 Kudos
StephenKruzik
New Contributor III
that did the trick.

thanks so much!!