1

I am looking to generate a report of all the fields for each feature class/table in an ArcSDE database. I'm using ArcGIS Desktop 10.2. I've written the following code, which gets the job done, but it isn't pretty code, or a pretty output .txt file.

The output is a text file, but what other format would be easy for non-GIS/non-techy users to access/view?

I thought about writing to excel and doing a tab for every FC, but that wouldn't be super user friendly. I am wondering if I could get some advice on how to pretty up the output, or if there is a previous question that covers this topic.

Also, it is important to note that I have some fields with aliases that are over 80 characters long, so the way I currently format the output .txt file results in a very long and unprintable file.

Here's my entire code:

import arcpy, os

#Write script that outputs the field name, alias, type, length, etc. 
#For every feature class and table in my proposed tables
docPath = 'C://GIS//GDB_Summary.txt'
doc = open(docPath, 'w') 

arcpy.env.workspace = 'Database Servers\NB2498_AARONGIS.gds\Equipment_Inventory (VERSION:dbo.DEFAULT)'
fcList = arcpy.ListFeatureClasses(feature_dataset='Equipment_Inventory.DBO.Inventory')
tableList = arcpy.ListTables()
message = ''

# Find the max length of any value that will appear in the output
lengthList = []
for fc in fcList:
    for field in arcpy.ListFields(arcpy.Describe(fc).name):
        lengthList.append(field.name)
        lengthList.append(field.aliasName)
        lengthList.append(field.type)
        lengthList.append(field.length)
        lengthList.append(field.domain)
for table in tableList:
    for field in arcpy.ListFields(arcpy.Describe(table).name):
        lengthList.append(field.name)
        lengthList.append(field.aliasName)
        lengthList.append(field.type)
        lengthList.append(field.length)
        lengthList.append(field.domain)

# Set the length of spaces between values and the length of the div lines        
spaceLength = (max(len(str(s)) for s in lengthList))
divLine_Length = spaceLength*5
indent = ' ' *(spaceLength//3)
n = '\n'
message += '\n***** Feature Classes ' + '*'*(divLine_Length-23)

# Loop through feature classes and add their values to message
for fc in fcList:
    desc = arcpy.Describe(fc)
    name = desc.name
    header = n + name + n + '-'*divLine_Length + n
    message += header
    columnList = ['Field Name', 'Field Alias', 'Field Type', 'Field Length', 'Field Domain']
    for column in columnList:
        if column == 'Field Name':
            spacing = ' '*(spaceLength-int(len(column)))
            message += column + spacing             
        else:
            spacing = ' '*(spaceLength-int(len(column)))
            message += indent + column + spacing 
    message += n
    for field in arcpy.ListFields(name):
        # Field Name
        spacing = ' '*(spaceLength-int(len(field.name)))
        message += field.name + spacing
        # Field Alias
        spacing = ' '*(spaceLength-int(len(field.aliasName))) 
        message += indent + field.aliasName + spacing
        # Field Type
        spacing = ' '*(spaceLength-int(len(field.type))) 
        message += indent + field.type + spacing  
        # Field Length
        spacing = ' '*(spaceLength-int(len(str(field.length)))) 
        message += indent + str(field.length) + spacing
        # Field Domain
        spacing = ' '*(spaceLength-int(len(field.domain))) 
        message += indent + field.domain + spacing + n

# Loop through tables and add their values to message
message += '\n***** Tables ' + '*'*(divLine_Length-13)
for table in tableList:
    desc = arcpy.Describe(table)
    name = desc.name
    header = n + name + n + '-'*divLine_Length + n
    message += header
    columnList = ['Field Name', 'Field Alias', 'Field Type', 'Field Length', 'Field Domain']
    for column in columnList:
        if column == 'Field Name':
            spacing = ' '*(spaceLength-int(len(column)))
            message += column + spacing             
        else:
            spacing = ' '*(spaceLength-int(len(column)))
            message += indent + column + spacing 
    message += n
    for field in arcpy.ListFields(name):
        # Field Name
        spacing = ' '*(spaceLength-int(len(field.name)))
        message += field.name + spacing
        # Field Alias
        spacing = ' '*(spaceLength-int(len(field.aliasName))) 
        message += indent + field.aliasName + spacing
        # Field Type
        spacing = ' '*(spaceLength-int(len(field.type))) 
        message += indent + field.type + spacing  
        # Field Length
        spacing = ' '*(spaceLength-int(len(str(field.length)))) 
        message += indent + str(field.length) + spacing
        # Field Domain
        spacing = ' '*(spaceLength-int(len(field.domain))) 
        message += indent + field.domain + spacing + n

# Write to text file, save it, and open the file to view
#print(message)
doc.write(message)
doc.close()
os.startfile(docPath)
1
  • For code reviews there is the Code Review Stack Exchange. For focussed Q&A we will need you to present just a code snippet that is not working rather than your whole code.
    – PolyGeo
    Jul 13, 2017 at 20:32

0

Browse other questions tagged or ask your own question.