I have a python (arcpy) script that is pulling values from a input list of fields for every featureclass in a geaodatabase (gdb) using a SearchCursor and is putting those values in a csv. The script works perfect for all featureclasses that have all the fields in the list. The script below is a thought process of how to capture all the field names in a list and do something if they are all present. This works but hits a stop if a field does not exist.
I've handled the " A column specified does not exist" using try/except exception in a different script that does not use the listFields so the script continues to run, but if even a single field does not exist in that featureclass the entire featureclass is skipped and is not present in the csv.
I need all records from all featureclasses in the csv even if a field does not exist in that featureclass. So something like if fieldname not in fieldList put "field does not exist" where otherwise that fields value would be. I need this for every field in the list. The csv will have featureclassname, field1, field2, field3..etc.
import arcpy import csv import operator arcpy.env.workspace = r"path\to\my\gdb" newCSV = r"path\to\new\csv" valueList =  # Use the ListDatasets function to return a list of all feature datasets in the gdb: datasetList = arcpy.ListDatasets("*", "Feature") with open(newCSV,'wb') as csvfile: for dataset in datasetList: # Use the ListFeatureClasses function to return a list of all fc's in the gdb: fcList = arcpy.ListFeatureClasses("*","",dataset) for fc in fcList: fieldname = ['field1', 'field2', 'field3'] fieldList = [f.name for f in arcpy.ListFields(fc)] if fieldname in fieldList and fieldname in fieldList and fieldname in fieldList: with arcpy.da.SearchCursor(fc, fieldname) as cursor: row = cursor.next() while row: valueList.append([[fc,row,row,row]]) # #merge lists of lists flattenList = reduce(operator.concat,valueList) writer = csv.writer(csvfile) writer.writerows(flattenList) row = cursor.next() valueList =