Calculate Field not working in arcpy

3314
11
Jump to solution
07-27-2016 02:33 AM
SolanaFoo4
New Contributor III

Not sure why this statement wouldn't run within an edit session in arcpy:

edit = arcpy.da.Editor(gdb)

edit.startEditing(False, True)

edit.startOperation()

arcpy.CalculateField_management ('Export_Output', 'Point_Type', 'UNKNOWN')

edit.stopOperation()

edit.stopEditing(True)

I am working with a versioned replica in SQL Express.  Working on the default version.

Solution: I just ended up using da.UpdateCursor to calculate all recs to 'UNKNOWN'.  This worked fine.  Was getting strange results otherwise (on small and large datasets).

0 Kudos
11 Replies
SolanaFoo4
New Contributor III

Interesting finding again, with this function.  Tried running:

arcpy.MakeFeatureLayer_management(fc,"cmpl","Material IN ('CMP',' CMP')")

arcpy.CalculateField_management("cmpl","Pipe_Material","'CMP'")

on some sde data unversioned (so no need to start an edit session).  It wouldn't run the calculate without error.  I had to add what they say is an optional parameter, expression_type: "PYTHON_9.3" to make it run.  

0 Kudos
RichardFairhurst
MVP Honored Contributor

I have gotten so used to Python not caring whether I use single quotes or double quotes that I forgot you cannot use single quotes with VB Script.  In VB Script you have to write the calculation with escapes.  The following works (determined by creating a model and exporting it to a Python script):

arcpy.CalculateField_management("cmpl","Pipe_Material","\"CMP\"")

Since this is not pure Python or pure VB Script it is not always easy to figure out where the Python parser of the script handles quotes and where the VB Script parser of the calculation handles quotes, so I usually build a model and export it to a Python script to get a reliable syntax.

0 Kudos