AnsweredAssumed Answered

CSV.DictWriter()Error

Question asked by mthornton@blm.gov_BLM_EGIS on Aug 20, 2019

I am attempting to create a csv file containing field_names as headers on the first row and input attributes under those field_Names column wise. First, I iterate through a list of data_sets: then i iterate through a list of feature_classes, search for specific field_names, and return the field_name value first found to match. I end up with a large list [] containing about 40 dicts{} and these dicts{} in turn contain each one a key and a list[] as the value pair: that list being the attributes found under the header(i.e.Field_Name that matched). I also have a function that transposes the data to be input column wise regardless of the attr. data list size, but for some reason I am getting an error: ValueError:dict contains fields not in fieldnames: Which is from the csv.DictWriter() function, but I have specified the fieldnames = headers in the function. In regards to the error it is looking at the first item in each attr. list[] conatined in each dict{}, but why is it looking for 'fieldnames' there? these are the values not the keys. Please help

#function to transpose data col-wise

def transposeANDwriteCSV(csvwriter, data) :

    maxLength = 0 # init maxLength to zero

    for item in data : # loop through the data list
        myKeysView = item.keys() # find the single key in each item (IS IT POSSIBLE THAT A KEY DOESN'T EXIST - MAY NEED TO CHECK)
        myKeys = list(myKeysView)
        myList = item.get(myKeys[0]) # retrieve the list in each item (IS IT POSSIBLE THAT A LIST FOR THIS KEY DOESN'T EXIST )
        if (len(myList) > maxLength) : # Check if the length of this list is the longest one.
            maxLength = len(myList) # If it is the longest then save.
# then loop through the main data list maxLength number of times

    for i in range (0,maxLength) : #loop through the entire data list until you have retrieved all the data
        myRow = [] #create a row list tthat you will build each pass through the loop
        for item in data :
            myKeysView = item.keys() # find the single key in this item
            myKeys = list(myKeysView)
            myList = item.get(myKeys[0]) # retrieve the list in this item
            if(len(myList) >= (i + 1) ) : #check if this list still has data remaining to retrieve
                myRow.append(myList[i]) # if it still has data then retrieve it and put into the row.
            else :
                myRow.append("") # if no data remains in this list then fill next cell in row with blank.

        csvwriter.writerow(myRow) # write row each time through maxLength loop

#function to find a field_name match
def FindField(fc):
    field_list = [
        "NAME", "PROJECT_NAME", "LABEL",  "Seed_Area", "SEED_MIX",  "MINENAME", "ORIGINAL_L", "WETLAND_TYPE",
         "PDF_NAME", "User_", "OwnerName", "Lessee",  "Plot_Name", "Plant_comm", "PitName", "Quad",
          "NOTES", "Range", "User", "ObjectID"
    ]
    fields = [fld.name.upper() for fld in arcpy.ListFields(fc)] #returns all filenames for current fc
    return next(f for f in field_list if f.upper() in fields)



##def createCSV(data, csvname, mode ='ab'):
##    with open(csvname, mode) as csvfile:
##        csvwriter = csv.writer(csvfile, delimiter="'")
##        csvwriter.writerow([data])


import arcpy
import csv
import os

csvname = r"U:\My Documents\ArcGIS\ArcPyOutputFiles\class1(type1)PolyAttrsss.csv"

arcpy.env.workspace = r"V:\tools\ArcSDE\Database Connections\5 NMSO ilmnmso3gi1 ilmnmsoclass1 (Type 1) Default.sde"

datasets = arcpy.ListDatasets("*FFO*", "Feature")
datasets = [''] + datasets if datasets is not None else []

headers = []
data=[]

#iterate through a list of datasets, then through a list of feature_classes
for ds in datasets:
    for fc in arcpy.ListFeatureClasses("*FFO*", "Polygon", feature_dataset=ds):
        path = os.path.join( ds, fc)
        headers.append(path)

        dataDict = {}
        colData = []

        FldName = FindField(fc)
##        createCSV(FldName, csvname)
        colData.append(FldName)

        with arcpy.da.SearchCursor(fc, [FldName]) as sc:
            for row in sc:
                name = row[0]
                colData.append(name)
##                createCSV(name, csvname)
        dataDict[path] = colData
        data.append(dataDict)

##print (data)
####print headers
##head = list(dataDict.keys())
##print head
##head = list(sorted(dataDict.keys()))
##print head
##
with open(csvname, mode = 'wb') as csvfile:
    csvwriter = csv.DictWriter(csvfile, delimiter=",", fieldnames = headers)
    csvwriter.writeheader()
    transposeANDwriteCSV(csvwriter, data)
##    csvwriter.writerows(zip(*data))

Outcomes