Return a list of attributes properties

1754
15
08-14-2019 04:04 PM
MichaelThornton
New Contributor II

I am attempting to write arcpy script that will iterate through an .sde connection database, finds all datasets containing polygons and the text "FFO" in the title, then selects for those and iterates through the feature_layers contained within.

I can do this with :

import arcpy
import csv
import os


csvname = r"U:\My Documents\ArcGIS\ArcPyOutputFiles\TestFile4.csv"
headers = 'Name', 'Path'
createCSV(headers, csvname, 'wb') 

arcpy.env.workspace = r"V:\tools\ArcSDE\Database Connections\5 NMSO ilmnmso3gi1 ilmnmsoclass1 (Type 1) Default.sde"

datasets = arcpy.ListDatasets("*FFO*", "Feature")
datasets = [''] + datasets if datasets is not None else []

for ds in datasets:
      for fc in arcpy.ListFeatureClasses("*FFO*", "Polygon", feature_dataset=ds):

         path = os.path.join( ds, fc)
         #print(path).

But there are multiple polygons in each feature layer, so I want to iterate through those as well. Which I can do with arcpy.da.SearchCursor() as long as the feature layers contain a similar field_name. But not all contain the word  "NAME". So I thought this would work.

   

   

arcpy.env.workspace = r"V:\tools\ArcSDE\Database Connections\5 NMSO ilmnmso3gi1 ilmnmsoclass1 (Type 1) Default.sde"

datasets = arcpy.ListDatasets("*FFO*", "Feature")
datasets = [''] + datasets if datasets is not None else []

for ds in datasets:
      for fc in arcpy.ListFeatureClasses("*FFO*", "Polygon", feature_dataset=ds):

            path = os.path.join( ds, fc)
            #print(path)

            desc = arcpy.Describe(fc)

            if hasattr(desc, 'name'):

              with arcpy.da.SearchCursor(fc, ['NAME']) as sc:
                  for row in sc:
                        Name = row[0]
                        Path = row[1]
                        data = Name, path
                        createCSV(data, csvname)

But it doesn't work. I want to load a csv file with a path to the data and an identifying name of each polygon contained in the feature layers, contained in the datasets. Thank you.

0 Kudos
15 Replies
JoshuaBixby
MVP Esteemed Contributor

Ravi Dhulipudi‌, it looks like you updated the code in your original reply.  Unfortunately, the updated code will generate the same AttributeError of the original code.  A data access search cursor (da.SearchCursor) returns a Python tuple each time it is advanced/called, and tuples don't support attribute lookup for items stored in them.

0 Kudos
by Anonymous User
Not applicable

I use (cursor = gp.searchCursor(fc, ["Col1", "Col2"])) in my current code and it works without any issues. I haven't tested with da.SearchCursor, just took it from the original question and provided as a sample code to read the attributes. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

could we use code formatting please to help with reading and especially to provide line numbers

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
MichaelThornton
New Contributor II

Thanks for the support here. The code below should work and it does to a point. I need a function that checks for various names the same way (if len(arcpy.ListFields(fc,"NAME"))>0:) checks for the Field_Name : 'NAME' and then assigns that to a variable(e.g FldName) to be used in the( with arcpy.da.SearchCursor(fc, ['FldName']) as sc:) call. I will need to only return the variable: 'path' if there is no appropriate  Field_Name found. However, I cannot figure out why the csv function is throwing an error or putting individual letters in each cell on the row in the xl doc. 

def createCSV(data, csvname, mode ='ab'):
    with open(csvname, mode) as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerow(data)


import arcpy
import csv
import os


csvname = r"U:\My Documents\ArcGIS\ArcPyOutputFiles\TestFile4.csv"
headers = 'Name', 'Path'
createCSV(headers, csvname, 'wb')

arcpy.env.workspace = r"V:\tools\ArcSDE\Database Connections\5 NMSO ilmnmso3gi1 ilmnmsoclass1 (Type 1) Default.sde"

datasets = arcpy.ListDatasets("*FFO*", "Feature")
datasets = [''] + datasets if datasets is not None else []

for ds in datasets:
    for fc in arcpy.ListFeatureClasses("*FFO*", "Polygon", feature_dataset=ds):

        path = os.path.join( ds, fc)

        if len(arcpy.ListFields(fc,"NAME"))>0:
##            print(path)
            with arcpy.da.SearchCursor(fc, ['NAME']) as sc:
                for row in sc:
                    data = row[0]
                    createCSV(data, csvname)

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In terms of path, do you want the path relative to the EGDB?  I assume you don't want a full path including the SDE connection file since there is not a one-to-one relationship between EGDBs and connection files, i.e., numerous SDE connection files in various locations can all point to the same EGDB.

0 Kudos
MichaelThornton
New Contributor II

Correct. I am not concerned with the SDE path, only the EGDB and feature class path. My goal is to get a print out of all polygons contained in EGDB's with the text "FFO" in the title. Since there are multiple polygons within my feature classes I will need to get in there to print out the individual polys, but it is a bit tricky since there are not good data standards in place and therefore it is not obvious what fieldname contains attributes that will give me pertinent information. Otherwise it is days of digging through multiple EGDB's and feature classes. 

0 Kudos