1

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)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 9, 2014 at 20:19
3
  • 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. Commented Jul 9, 2014 at 20:30
  • @Erica Thanks, that gave me a push in the right direction. I got it to work. Commented 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 :) Commented Jul 9, 2014 at 20:53

1 Answer 1

3

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)
answered Jul 9, 2014 at 21:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.