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)
  • 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 Jul 9 '14 at 20:30
  • @Erica Thanks, that gave me a push in the right direction. I got it to work. – Kat Jul 9 '14 at 20:46
  • OK great -- I was just working up a little script to test it myself, but I'll stop now :) – Erica Jul 9 '14 at 20:53
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)

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.