Accessing Coded Domain Values using python

9189
3
Jump to solution
04-11-2012 11:12 AM
MikeMacRae
Occasional Contributor III
I am trying to find a way to access domain codes and descriptive values using python for each record in a given field. I have a domain code set for a few fields in a feature class. The user will populate the records for each field using the coded values I set. When that is done, I want to access the value they chose in the attribute table using python. I tried the following:

for row in arcpy.SearchCursor("TESTFC")     print row.TestField1


This returns the coded value if a record was populated by the domain code and it prints out 'None' if the record is Null. I'm wondering if you can return the descriptive value for the domain code?


Thanks,
Mike
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RussellBrennan
Esri Contributor
The following code should help.

I put in an excessive amount of comments to make this clear as I don't know your skill level with Python.

import arcpy  #Create a table in memory to hold the codes and their descriptions for a particular domain. cvdTable = arcpy.DomainToTable_management(r'C:\vancouver.gdb', 'type', 'in_memory/cvdTable', 'codeField', 'descriptionField')  #Create an empty dictionary. d = {}  #Create a cursor to loop through the table holding the domain and code info rows = arcpy.SearchCursor(cvdTable)  # Loop through each row in the table. for row in rows:   # For each row in the table populate the key with the code field and the value from the description field   d[row.codeField] = row.descriptionField #Cleanup del row del rows  # Create a search cursor on the table with the domain rows2 = arcpy.SearchCursor(r'C:\vancouver.gdb\TESTFC') # Loop through the records in the table. for row2 in rows2:   # Print the dictionary value for each code that is returned from this search cursor   print d[row2.PipeType] #Cleanup del row del rows 


Hope this helps.

View solution in original post

3 Replies
RussellBrennan
Esri Contributor
The following code should help.

I put in an excessive amount of comments to make this clear as I don't know your skill level with Python.

import arcpy  #Create a table in memory to hold the codes and their descriptions for a particular domain. cvdTable = arcpy.DomainToTable_management(r'C:\vancouver.gdb', 'type', 'in_memory/cvdTable', 'codeField', 'descriptionField')  #Create an empty dictionary. d = {}  #Create a cursor to loop through the table holding the domain and code info rows = arcpy.SearchCursor(cvdTable)  # Loop through each row in the table. for row in rows:   # For each row in the table populate the key with the code field and the value from the description field   d[row.codeField] = row.descriptionField #Cleanup del row del rows  # Create a search cursor on the table with the domain rows2 = arcpy.SearchCursor(r'C:\vancouver.gdb\TESTFC') # Loop through the records in the table. for row2 in rows2:   # Print the dictionary value for each code that is returned from this search cursor   print d[row2.PipeType] #Cleanup del row del rows 


Hope this helps.
MikeMacRae
Occasional Contributor III
Hey Russell, thanks for the post!

I actually forgot about this post, but started working on a solution. I came across the domain to table tool and I wrote a script and it works very well. It's similar to what you posted. In my case, I just generated the tool into a scratchworkspace, read from it while my tool processes and then deleted it. The domain tools have fixed a lot of my issues!

Thanks,
Mike
by Anonymous User
Not applicable

Just adding this here because this was the top hit in Google for my first search on the same topic

https://pro.arcgis.com/en/pro-app/tool-reference/environment-settings/transfergdbattributeproperties...

You can leverage environment settings to do this now instead of the dictionary repeat