Select to view content in your preferred language

How to get the attribute table value of a particular field where the field values are domain assigned values using arcpy

541
6
05-01-2023 05:18 AM
kumarprince8071
New Contributor III

This is the attribute table values of two fields but the values inside the fields are domain coded description values by using search cursor i am getting only the code not the values need help or suggestions for that.

kumarprince8071_0-1682943468084.png

 

Tags (3)
6 Replies
by Anonymous User
Not applicable

Use the list domains and find it for your featurclass:

domains = arcpy.da.ListDomains("C:/Boston/Boston.gdb")

for domain in domains:
    print(f"Domain name: {domain.name}")
    if domain.domainType == "CodedValue":
        coded_values = domain.codedValues
        for val, desc in coded_values.items():
            print(f"{val} : {desc}")

 

kumarprince8071
New Contributor III

I have tried this way but i am getting all the domain coded values for that but i need only the field values like shown in the image. Kindly give me some suggestions on that please.

0 Kudos
by Anonymous User
Not applicable

Filter on the field?

fc = r'path to your fc'

field = 'Field you want'

target_field = [f for f in arcpy.ListFields(fc) if field == f.name]

if target_field[0].domain:
    field_domain = [d for d in arcpy.da.ListDomains(arcpy.Describe(fc).path) if d.name == target_field[0].domain][0]
    
    for k,v in field_domain.codedValues.items():
        print(f'domain key: {k}  domain value: {v}')
kumarprince8071
New Contributor III

Traceback (most recent call last):
File "D:\ARCPY\ArcPy_New\feature_exists.py", line 182, in <module>
if target_field[0].domain:
IndexError: list index out of range

Getting this error

Attaching the code also

kumarprince8071_0-1682955618370.png

Here one more information is that ASSETGROUP field is a subtype field.

0 Kudos
by Anonymous User
Not applicable

Instead of sending a list of fields, send a single field.

 

kumarprince8071
New Contributor III

Yes, i tried it also but i'm not getting any result also not getting any error.

0 Kudos