I have a database with multiple feature classes. Now I extracted a list of feature classes and a list of fields. I figured out how to write the list of feature classes and the corresponding fields to csv. But the csv file gets really long. Instead I would like to write each feature class name with the corresponding field names to a separate csv file. My code looks like this right now:
def csvwriter (csvfile, mylist):
myfile = open(csvfile,'ab')
wr = csv.writer(myfile)
wr.writerow(mylist)
datasets = arcpy.ListDatasets()
for ds in datasets:
dslist = []
dslist.append(ds)
csvwriter (csvfile1,dslist)
for fc in arcpy.ListFeatureClasses("*","",ds):
fclist = []
fclist.append(fc)
csvwriter(csvfile1,fclist)
for field in arcpy.ListFields(fc):
fieldlist = []
fieldlist.append(field.name)
csvwriter(csvfile1,fieldlist)
-
You'll need to change the CSV file name to a variable that depends on the feature class, either changing based on an index value (e.g. 1, 2, 3) or based on the feature class name.Erica– Erica2014年07月09日 20:30:35 +00:00Commented Jul 9, 2014 at 20:30
-
@Erica Thanks, that gave me a push in the right direction. I got it to work.Kat– Kat2014年07月09日 20:46:02 +00:00Commented Jul 9, 2014 at 20:46
-
OK great -- I was just working up a little script to test it myself, but I'll stop now :)Erica– Erica2014年07月09日 20:53:24 +00:00Commented Jul 9, 2014 at 20:53
1 Answer 1
Instead of using the same csvfile1
each time, define a variable that depends on the feature class name. For example:
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
csvFileName = 'CSV_{}.csv'.format(fc) # or just = fc + '.csv'
for field in arcpy.ListFields(fc):
csvwriter(csvFileName,field.name)
Explore related questions
See similar questions with these tags.