Select to view content in your preferred language

Export to .TXT file include domain values

680
4
07-26-2022 09:44 AM
ModernElectric
Occasional Contributor III

I have already created a series of scripts to sort thru my GDB and produce infrastructure statistics. I have developed one script to run all of the scripts and export the results to a series of .TXT files:

import arcpy
import sys
import os.path

arcpy.env.transferDomains = True

orig = sys.stdout
with open(os.path.join("Z:\Operations\Maps and Records\Infrastructure Counts Stats\DATA EXPORT", "eJOINT_USE_INVENTORY.txt"), "wb") as f:
    sys.stdout = f
    try:
        execfile("JOINT_USE_COUNTS_NEW.py", {})
    finally:
        sys.stdout = orig

The issue I am having is when this exports the File GDB Table to a .TXT value, the domain code is exported. I need to have the domain description when the data is exported to the .TXT file. I have verified that the domain is assigned to the specific field.

Thank You

0 Kudos
4 Replies
RhettZufelt
MVP Notable Contributor

so, what does the code that actually exports the table look like?  What tool is it using

all I see here is standard python writing to a text file.  arcpy is where the domains would be handled.

R_

0 Kudos
ModernElectric
Occasional Contributor III

 

## CENTURY LINK CONTACTS - PREPARE AND PRINT STATS
print("## CENTURY LINK - JOINT USE - COUNTS ##")
fc = "C:\Users\cwafstet\Documents\GIS WORKING FILES LOCAL\ELECTRIC\MEWCo ELECTRIC SYSTEM - DATA PREP.gdb\STATS_JU_CENTURYLINK"
total = 0
fields = ['CHARGE_TYPE','FREQUENCY']
with arcpy.da.SearchCursor(fc,fields) as cursor:
	for row in cursor:
		total += row[1]
		print "There are {} features for {}".format(row[1],row[0])

This is the python script the creates the statistics and write it to a File GDB Table. I verify the domain is added to the field within ArcCatalog. The field with the domain added is CHARGE_TYPE. When the data is printed to the Python Idle screen AND/OR the .TXT file, the code for the domain is displayed instead of the domain description.

ModernElectric_0-1658856623531.png

ArcCatalog: CHARGE_TYPE field showing the Domain Description

ModernElectric_1-1658856652358.png

.TXT exported file showing the domain code. What is circled in red, the goal is to have the domain description instead.

 

0 Kudos
RhettZufelt
MVP Notable Contributor

That is the python code that iterates through an existing table and prints the field values.

How/where did that table come from?  Statistics? Summary?.....  I can't seem to create a similar table that keeps the domains for testing.

However it is generated, one option is to export/save it to a stand alone text file INSTEAD of a FGDB table, then search cursor the text file for the print part.  Stand alone text files aren't in a GDB, so they don't carry the domains with them.

R_

 

0 Kudos
by Anonymous User
Not applicable

Edited to fix the domain dictionary.

----

Put the corresponding values into a dictionary: domain_dict = {150: 'Contact', 151:'Guy', 152:'Riser', ...}

then do a dictionary lookup for the value in the SearchCursor:

 

with arcpy.da.SearchCursor(fc,fields) as cursor:
	for row in cursor:
	    total += row[1]
            dv = domain_dict.get(row[0], 'Unknown') # <- gets the domain value name here or assignes 'Unknown' as the default value
	    print "There are {} features for {}".format(row[1],dv)

 

... if you have a lot of values- you can maybe dynamically create the domain dict, but the key would need to be the number somehow if the domain is a 'Contact': Contact' instead of a 150: 'Contact'.  This example is for setting the key as an index 0, 1, 2, etc, but you may be able to get away with using domain.codeValues directly if the domain is 150: 'CONTACT' format.

 

domains = arcpy.da.ListDomains("path to your .gdb")
domain_dict ={}

for domain in domains:
    print('Domain name: {0}'.format(domain.name))
    if domain.name == 'THE NAME OF THE DOMAIN FOR THAT FIELD':
        if domain.domainType == 'CodedValue':
            for i, v in enumerate(domain.codedValues.values()):
                domain_dict[i] = v

 

0 Kudos