Select to view content in your preferred language

Calculate Field not working in arcpy

3842
11
Jump to solution
07-27-2016 02:33 AM
SolanaFoo4
Regular Contributor

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
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor

Do this for the calculation:

arcpy.CalculateField_management ("swXSectionPoint", "Point_Type", '"UNKNOWN"')

Notice that the expression cannot be "UNKNOWN", it must be '"UNKNOWN"' (single quote, double quote, UNKNOWN, double quote, single quote).

This assumes that swXSectionPoint is a layer recognized by the script and that Point_Type is a field in that layer.  There is no bug and support will tell you to write the calculation expression the same as what I have written in this post.

View solution in original post

0 Kudos
11 Replies
RichardFairhurst
MVP Honored Contributor

You forgot the quotes for the expression.  It needs to be '"UNKNOWN"'  Quotes are always the first thing to check whenever you do calculations, especially when they are run in a script.

0 Kudos
SolanaFoo4
Regular Contributor

So single quotes don't work here?

0 Kudos
RichardFairhurst
MVP Honored Contributor

No, that is not the problem.  It is that it must quote a quoted string, since the expression in the normal field calculator must be quoted.  "UNKNOWN" is what you should enter in the field calculator outside of a script.  Inside a script that quoted string needs to be quoted again to send it as a string value with quotes.

0 Kudos
SolanaFoo4
Regular Contributor

So I did use quotes, but just single quotes to quote the string.

0 Kudos
RichardFairhurst
MVP Honored Contributor

In a script you have to quote the expression regardless of what the value inside it contains (field names, numbers and strings all have to be in quotes).  So 'UNKNOWN' equates to an unknown value of UNKNOWN (no quotes) to the field calculator, which does not exist and causes an error.  You want to sent "UNKNOWN" (a quoted string), so you have to put that in quotes in the script, i.e., ' "UNKNOWN" ' (works for VB Script or Python calculations) or " 'UNKNOWN' " (works only for Python calculations).

0 Kudos
SolanaFoo4
Regular Contributor

Hm, I tried this:

edit = arcpy.da.Editor(gdb)

edit.startEditing(False, True)

edit.startOperation()

# with arcpy.da.UpdateCursor('swXsectionPoint','Point_Type') as unk_csr:

#     for unk_rec in unk_csr:

#         unk_rec[0]="UNKNOWN"

#         unk_csr.updateRow(unk_rec)

arcpy.CalculateField_management ("swXSectionPoint", "Point_Type", "UNKNOWN")

edit.stopOperation()

edit.stopEditing(True)   

And it is still giving me strange results.  Tried doing it out of an edit session as well and still no luck.  Also, the Update Cursor shown commented out above seems to work.  Will put in a support request.  Perhaps I need to compress the gdb, or maybe I found a bug.

0 Kudos
RichardFairhurst
MVP Honored Contributor

Do this for the calculation:

arcpy.CalculateField_management ("swXSectionPoint", "Point_Type", '"UNKNOWN"')

Notice that the expression cannot be "UNKNOWN", it must be '"UNKNOWN"' (single quote, double quote, UNKNOWN, double quote, single quote).

This assumes that swXSectionPoint is a layer recognized by the script and that Point_Type is a field in that layer.  There is no bug and support will tell you to write the calculation expression the same as what I have written in this post.

0 Kudos
SolanaFoo4
Regular Contributor

Ah okay!  Thank you!

0 Kudos
SolanaFoo4
Regular Contributor

Ah that did it!  Support thought it was a bug too, but they realized after looking further that it was a SQL issue.  I have no idea how this has not been an issue for me in the past.  I don't remember double-quoting my strings.

0 Kudos