I'm using matt wilkies' arcplus module https://gis.stackexchange.com/a/5943/16793 to export a list of feature classes within an SDE database to a text file. I've also written a tool to write field names to a csv file:
"""The source code of the tool."""
import arcpy, os, csv
fclass = "SDEConnection\\"+Feature
fieldnames = [f.name for f in arcpy.ListFields(fclass)]
writepath = r"C:\Users\ME\Desktop\TableFields\\"+Feature+".csv"
with open(writepath, "w") as output:
writer = csv.writer(output, lineterminator='\n')
for val in fieldnames:
writer.writerow([val])
Ideally, I'd like to loop through the feature classes and fields and write a write file containing something like below:
Feature Class or Table-----------------------------------Fields
WELLS.WELL_DATA-------------------------------------WELL_KEY, SITEID, DEPTH, DIAMETER
I'm a little stumped on nesting the loops and formatting the output like this.
-----------EDIT-----------------------------------------------------------
Using the Walk method with a script like this
import os, arcpy, csv
def inventory_data(workspace, datatypes):
for path, path_names, data_names in arcpy.da.Walk(
workspace, datatype=datatypes):
for data_name in data_names:
yield os.path.join(path, data_name)
obtains a unicode string like "C:\Users\user\AppData\Roaming\Esri\Desktop10.2\ArcCatalog\Connection to oracle.sde\BASINS\BASINS.ONE"
while
fn = [f.name for f in arcpy.ListFields(fc)]
print fn
extracts a list
[u'FNODE_', u'TNODE_', u'LPOLY_', u'RPOLY_',]
My aim is to combine Walk with ListFields in a loop and write the FeatureClass with its accompanying fields (delimited) to a file.
-
I asked a similar question here which outputs to a text file if a certain field contains a link that doesn't exist within the database. gis.stackexchange.com/questions/76900/…GISHuman– GISHuman2014年07月31日 18:07:16 +00:00Commented Jul 31, 2014 at 18:07
2 Answers 2
Here's an example you can reference. It prints the results to both the console and to a text file, but you could easily manipulate it to write to a .csv file.
Hope this helps.
-ST
Results :
Feature Class Name : CR.CANADA1
Geometry Type : Polygon
Has Spatial Index : True
Spatial Reference : GCS_North_American_1927
Field Name : CR.CANADA1.AREA Field Type : Double Field Length : 8
Field Name : CODE Field Type : String Field Length : 4
Field Name : NAME Field Type : String Field Length : 25
Field Name : POP1991 Field Type : Double Field Length : 8
Field Name : POP91_SQMI Field Type : Double Field Length : 8
Field Name : SHAPE Field Type : Geometry Field Length : 4
Field Name : SHAPE.AREA Field Type : Double Field Length : 0
Field Name : SHAPE.LEN Field Type : Double Field Length : 0
Field Name : SHAPE.FID Field Type : OID Field Length : 0
# of Fields in Feature Class 'CR.CANADA1' = 9
CODE :
import arcpy
logfile = open(r'C:\TEMP\ListFCs_ListFieldNames_ListFieldTypes.log', 'w')
def log_results(message):
print(message)
logfile.write(message)
logfile.flush()
return
def main():
try:
wk = arcpy.env.workspace = 'Database Connections/ORCL - [email protected]'
fcList = arcpy.ListFeatureClasses("*")
fcList.sort()
if not arcpy.Exists(wk):
log_results("\n" + " SDE Connection File DOES NOT EXIST!!\n")
else:
fcCnt = 0
fldCnt = 0
for fc in fcList:
fcCnt += 1
desc = arcpy.Describe(fc)
log_results ("\n Feature Class Name : {0} \n Geometry Type : {1} \n Has Spatial Index : {2} \n Spatial Reference : {3}".format(fc,desc.shapeType,str(desc.hasSpatialIndex),desc.spatialReference.name))
#field = [f.name for f in arcpy.ListFields(fc)]
fields = arcpy.ListFields(fc)
fldCnt = 0
for field in fields:
log_results ("\n Field Name : {0: <24} Field Type : {1: <20} Field Length : {2}".format(field.name, field.type, field.length))
fldCnt += 1
log_results ("\n # of Fields in Feature Class '{0}' = {1} \n".format(fc, fldCnt))
log_results ("\n TOTAL # OF FEATURE CLASSES LISTED = {0}".format(fcCnt))
log_results ("\n\n COMPLETED!! \n")
logfile.close()
except arcpy.ExecuteError:
log_results (arcpy.GetMessages(2))
except Exception as e:
log_results (e[0])
if __name__ == '__main__':
main()
Loop through geodatabase (to get list of feature classes)
Use Walk (arcpy.da) method
Once you have the feature class you may want to verify data type (feature class, table,....etc) before you start the list field loop. To do this you may use the Describe Object property dataType. For checking data type you may reference this Q/A:
Get field list
Use the ListFields mehtod
-
I can use the walk method to generate a list of the feature classes, but I also want to use the ListFields method to loop through each feature class and ideally, generate a comma delimited list of fields in a separate columnCraig Freeman– Craig Freeman2014年07月31日 19:05:12 +00:00Commented Jul 31, 2014 at 19:05
Explore related questions
See similar questions with these tags.