1

New to Python. I'm trying to combine two scripts I found online that use the ListDatasets and ListFields tools. One loops through all the features of a geodatabase and writes the name of the feature dataset, feature class, and the number of features to a .csv file. To that one I would like to add the other that prints to the screen information about the attributes--name, type, and length. I would like all of this printed to a single .csv file like this:

enter image description here

The ListDatasets Script:

import arcpy

arcpy.env.workspace = "C:\Users\Redfish\Test.sde\Test.DBO.Structures\Test.DBO.HospitalBldg"


file = open("C:\Users\Redfish\Desktop\DatabaseDictionary.csv", "w")
file.write('Dataset, LayerName, NoOfRecords' + '\n')

for fds in arcpy.ListDatasets('*','feature') + ['']:
                for fc in arcpy.ListFeatureClasses('*','',fds):
                                result = int(arcpy.GetCount_management(fc).getOutput(0))
                                file.write(fds + ', ' + fc + ', ' + str(result) + '\n')

file.close()

The ListFields Script

import arcpy

arcpy.env.workspace = "C:\Users\Redfish\Test.sde\Test.DBO.Structures\Test.DBO.HospitalBldg"

fields = arcpy.ListFields("C:\Users\Redfish\Test.sde\Test.DBO.Structures\Test.DBO.HospitalBldg")
for field in fields:
    print("{0}\{1}\{2}"
          .format(field.name, field.type, field.length))
  • While you can use any indentation scheme in Python, you really ought to use four spaces. This will make your code consistent (and readable on small-screen devices on GIS SE). – Vince Oct 15 '19 at 16:44
0

In your first script fc is your feature class. After for fc in arcpy.ListFeatureClasses('*','',fds): add your ListFields script.

import arcpy

arcpy.env.workspace = "C:\Users\Redfish\Test.sde\Test.DBO.Structures\Test.DBO.HospitalBldg"


file = open("C:\Users\Redfish\Desktop\DatabaseDictionary.csv", "w")
file.write('Dataset, LayerName, NoOfRecords' + '\n')

for fds in arcpy.ListDatasets('*','feature') + ['']:
                for fc in arcpy.ListFeatureClasses('*','',fds):
                                result = int(arcpy.GetCount_management(fc).getOutput(0))
                                fields = arcpy.ListFields(fc)
                                #start gathering info
                                for field in fields:
                                    #add field info
                                    vals = [fds, fc, result, field.name, field.type, field.length]
                                    #write one line per field
                                    file.write(",".join (map (str, vals)) + "\n")
  • This is great! Thanks very much. I was pretty close. – Redfish Oct 15 '19 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.