arcpy calculatefieldmanagement domain field

582
11
06-16-2020 09:03 AM
BillChappell
Occasional Contributor II

Hi, using arcpy I'm trying to calculate a field that's using a domain. I'm actually calculating 2 fields, one after the other.

The first one works but the second one doesn't set it's new value. No error messages. If I'm not using python and just field calculate it "USER" puts "User-Derived" in the field. However my script just doesn't.

ArcMap 10.6 with a File GeoDataBase Featureclass

 

Any ideas?

try:
# Execute CalculateField

# this one works well.
arcpy.CalculateField_management(ug, "MEASUREDLENGTH", dist, "PYTHON_9.3")
arcpy.AddMessage("Calc'd MEASUREDLENGTH")

# this one is supposed to end up with "User-Derived" but doesn't do anything
arcpy.CalculateField_management(ug, "LENGTHSOURCE", "USER", "PYTHON_9.3")
arcpy.AddMessage("Calc'd LENGTHSOURCE")

Domain Key/Value:
FM Field Measurement
MS Mapping System
USER User-Derived
WM Windmil
WO Work Order Package

Tags (2)
0 Kudos
11 Replies
JoeBorgione
MVP Esteemed Contributor

Here is the syntax for the command:

CalculateField(in_table, field, expression, {expression_type}, {code_block}, {field_type})

It appears to me you are using  the domain name as the expression parameter.  Have you tried using the actual value of 'User-Derived' ?

That should just about do it....
0 Kudos
BillChappell
Occasional Contributor II

I've tried the value 'User-Derived' and the key 'USER', and no luck. I've used the key outside of python and it populates with the value but not in my script. 

I can populate the MEASUREDLENGTH field with a value in python without a problem, but that domain field is failing.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Arcmap 10.6 is the 64 bit flavor of python 2.7 as I recall.  I wonder two things and they are total WAGs (wild a$$ guess):

What if you drop the  "PYTHON_9.3"  reference altogether?

Could the '-' in User-Derived be derailing things?

When I was using 10.6 I do recall there were some bizarre gotchas in python that would goof our scheduled tasks.

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Joe Borgione‌, no version of ArcMap has or will come with 64-bit Python.  ArcMap is and will remain a 32-bit application bundled with 32-bit Python.  That said, if someone installs the Background Geoprocessing (64-bit) component, then they will have both a 32-bit and 64-bit Python for ArcGIS Desktop on their machine.

JoeBorgione
MVP Esteemed Contributor

they will have both a 32-bit and 64-bit Python for ArcGIS Desktop on their machine.

Ahh;  now I recall the problem I had; since I had pro installed on my desktop, I was developing my scripts we used for scheduled tasks in 64 bit python.  But...  The server that runs the  scheduled tasks only had 32 bit (and 2.x python) so things got messy for us, especially when migrating existing scripts to python 3.x...

That should just about do it....
0 Kudos
RandyBurton
MVP Regular Contributor

I like to test field calculations with the CalculateField tool and then look at the results window and 'copy as Python snippet'.  When this was done, I noticed that the domain code was wrapped in single quotes and then wrapped in double quotes.  So try:

arcpy.CalculateField_management(ug, "LENGTHSOURCE", "'USER'", "PYTHON_9.3")

I also noticed most of your domain codes are 2 characters.  If you still have issues, you may wish to confirm that 'USER' is the correct code.

JoshuaBixby
MVP Esteemed Contributor

Randy Burton‌, the single-quoted text strings are not unique to domains.  In ArcGIS Desktop/ArcMap, Calculate Field has always required Python strings to be wrapped in single quotes to work.

BillChappell
Occasional Contributor II

Not sure what caused my error, tried modelbuilder and exported that to python, While modelbuilder worked I noticed it had a couple intermediate variables created, and didn't work in my script. Leading me to believe I screwed up elsewhere and this was a result.

Frustrated I just decided to go with a updateCursor and that worked the first time. 

try:

    Ucursor = arcpy.UpdateCursor(oh, query)
    for row in Ucursor:
       for fieldname, value in (('MEASUREDLENGTH', '100'),
                                      ('LENGTHSOURCE', 'USER')):
          row.setValue(fieldname, value)
     Ucursor.updateRow(row)

.

JoeBorgione
MVP Esteemed Contributor

That's great you were able to export a model to python and get to work right out of the box!

That should just about do it....
0 Kudos