Select to view content in your preferred language

Want to add a list of domain values to a script that lists fields and field details for each feature class in a specified feature dataset

943
1
04-05-2023 04:12 PM
AudreyKilloran
New Contributor

Hello world!

I am trying to automate data dictionary creation, and am self-taught  when it comes to arcpy, so my scripting feels a bit like running blind sometimes... 

Where I'm at:

My colleague and I have created a script that outputs a csv that has the layer name, field name, alias, type, length, and domain table for feature classes in specified feature datasets.   I am running this in a notebook in ArcPro 3.0 using ArcPy 3.0.

#import modules and specify overwrite
import arcpy
import os
arcpy.env.overwriteOutput = True

arcpy.env.workspace = r"mygeodatabase.sde"

# variables:
#define csv variables and headings

headings = ["layer", "name", "alias", "type", "length", "field_domain"]
data = [headings]
outfile = r'C\\Users\myname\fileoutputlocation\outputfilename.csv'

#name csv tabs by fc and output to new tab or name stuff by FC/ds

#action stuff            
datasets = arcpy.ListDatasets(feature_type='feature')

for ds in datasets:
    if ds in ('Feature.dataset', 'other.dataset', 'other.otherdataset'):
        for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
         #List fields in feature class
            fields = arcpy.ListFields(fc)
            #Loop through fields
            for field in fields:
                #Check if field has domain
                row = [fc, field.name, field.aliasName, field.type, field.length, field.domain]
                data.append(row)

#output csv 
file = open(outfile, 'w', newline='')
with file:
  writer = csv.writer(file)
  writer.writerows(data)
print('CSV file written')

 

This is the output:

AudreyKilloran_0-1680735807672.png

Where I want to be:

The final step, my dear colleagues, would be to list the codes and code descriptions for the domains output by this script.  I'm not sure whether it could all be output in a specific column in the same csv, or as a separate tab on the csv for reference.

This is loosely what I want:

AudreyKilloran_1-1680735968073.png

I am stumped.  Any creative solutions?  Thanks in advance!

1 Reply
KimGarbade
Frequent Contributor

I think this object Domain—ArcGIS Pro | Documentation and this function ListDomains—ArcGIS Pro | Documentation are what I would try.  The result looks like a dictionary, so should be pretty easy for format.  Hope this helps.  K

0 Kudos