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
<SPAN class="comment token">#function to transpose data col-wise</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">transposeANDwriteCSV</SPAN><SPAN class="punctuation token">(</SPAN>csvwriter<SPAN class="punctuation token">,</SPAN> data<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">:</SPAN>
maxLength <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="comment token"># init maxLength to zero</SPAN>
<SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> data <SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># loop through the data list</SPAN>
myKeysView <SPAN class="operator token">=</SPAN> item<SPAN class="punctuation token">.</SPAN>keys<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># find the single key in each item (IS IT POSSIBLE THAT A KEY DOESN'T EXIST - MAY NEED TO CHECK)</SPAN>
myKeys <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>myKeysView<SPAN class="punctuation token">)</SPAN>
myList <SPAN class="operator token">=</SPAN> item<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>myKeys<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># retrieve the list in each item (IS IT POSSIBLE THAT A LIST FOR THIS KEY DOESN'T EXIST )</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>myList<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> maxLength<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Check if the length of this list is the longest one.</SPAN>
maxLength <SPAN class="operator token">=</SPAN> len<SPAN class="punctuation token">(</SPAN>myList<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># If it is the longest then save.</SPAN>
<SPAN class="comment token"># then loop through the main data list maxLength number of times</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range <SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>maxLength<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#loop through the entire data list until you have retrieved all the data</SPAN>
myRow <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#create a row list tthat you will build each pass through the loop</SPAN>
<SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> data <SPAN class="punctuation token">:</SPAN>
myKeysView <SPAN class="operator token">=</SPAN> item<SPAN class="punctuation token">.</SPAN>keys<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># find the single key in this item</SPAN>
myKeys <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>myKeysView<SPAN class="punctuation token">)</SPAN>
myList <SPAN class="operator token">=</SPAN> item<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>myKeys<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># retrieve the list in this item</SPAN>
<SPAN class="keyword token">if</SPAN><SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>myList<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">>=</SPAN> <SPAN class="punctuation token">(</SPAN>i <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#check if this list still has data remaining to retrieve</SPAN>
myRow<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>myList<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># if it still has data then retrieve it and put into the row.</SPAN>
<SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">:</SPAN>
myRow<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># if no data remains in this list then fill next cell in row with blank.</SPAN>
csvwriter<SPAN class="punctuation token">.</SPAN>writerow<SPAN class="punctuation token">(</SPAN>myRow<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># write row each time through maxLength loop</SPAN>
<SPAN class="comment token">#function to find a field_name match</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">FindField</SPAN><SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
field_list <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>
<SPAN class="string token">"NAME"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"PROJECT_NAME"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"LABEL"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Seed_Area"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"SEED_MIX"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"MINENAME"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ORIGINAL_L"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"WETLAND_TYPE"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"PDF_NAME"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"User_"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"OwnerName"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Lessee"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Plot_Name"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Plant_comm"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"PitName"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Quad"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"NOTES"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Range"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"User"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ObjectID"</SPAN>
<SPAN class="punctuation token">]</SPAN>
fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>fld<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">.</SPAN>upper<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> fld <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#returns all filenames for current fc</SPAN>
<SPAN class="keyword token">return</SPAN> next<SPAN class="punctuation token">(</SPAN>f <SPAN class="keyword token">for</SPAN> f <SPAN class="keyword token">in</SPAN> field_list <SPAN class="keyword token">if</SPAN> f<SPAN class="punctuation token">.</SPAN>upper<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">in</SPAN> fields<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">##def createCSV(data, csvname, mode ='ab'):</SPAN>
<SPAN class="comment token">## with open(csvname, mode) as csvfile:</SPAN>
<SPAN class="comment token">## csvwriter = csv.writer(csvfile, delimiter="'")</SPAN>
<SPAN class="comment token">## csvwriter.writerow([data])</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> csv
<SPAN class="keyword token">import</SPAN> os
csvname <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"U:\My Documents\ArcGIS\ArcPyOutputFiles\class1(type1)PolyAttrsss.csv"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"V:\tools\ArcSDE\Database Connections\5 NMSO ilmnmso3gi1 ilmnmsoclass1 (Type 1) Default.sde"</SPAN>
datasets <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListDatasets<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*FFO*"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Feature"</SPAN><SPAN class="punctuation token">)</SPAN>
datasets <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+</SPAN> datasets <SPAN class="keyword token">if</SPAN> datasets <SPAN class="keyword token">is</SPAN> <SPAN class="operator token">not</SPAN> None <SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
headers <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
data<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#iterate through a list of datasets, then through a list of feature_classes</SPAN>
<SPAN class="keyword token">for</SPAN> ds <SPAN class="keyword token">in</SPAN> datasets<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> fc <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFeatureClasses<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*FFO*"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Polygon"</SPAN><SPAN class="punctuation token">,</SPAN> feature_dataset<SPAN class="operator token">=</SPAN>ds<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
path <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN> ds<SPAN class="punctuation token">,</SPAN> fc<SPAN class="punctuation token">)</SPAN>
headers<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">)</SPAN>
dataDict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN>
colData <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
FldName <SPAN class="operator token">=</SPAN> FindField<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">## createCSV(FldName, csvname)</SPAN>
colData<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>FldName<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN>FldName<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> sc<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> sc<SPAN class="punctuation token">:</SPAN>
name <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
colData<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>name<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">## createCSV(name, csvname)</SPAN>
dataDict<SPAN class="punctuation token">[</SPAN>path<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> colData
data<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>dataDict<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">##print (data)</SPAN>
<SPAN class="comment token">####print headers</SPAN>
<SPAN class="comment token">##head = list(dataDict.keys())</SPAN>
<SPAN class="comment token">##print head</SPAN>
<SPAN class="comment token">##head = list(sorted(dataDict.keys()))</SPAN>
<SPAN class="comment token">##print head</SPAN>
<SPAN class="comment token">##</SPAN>
<SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>csvname<SPAN class="punctuation token">,</SPAN> mode <SPAN class="operator token">=</SPAN> <SPAN class="string token">'wb'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> csvfile<SPAN class="punctuation token">:</SPAN>
csvwriter <SPAN class="operator token">=</SPAN> csv<SPAN class="punctuation token">.</SPAN>DictWriter<SPAN class="punctuation token">(</SPAN>csvfile<SPAN class="punctuation token">,</SPAN> delimiter<SPAN class="operator token">=</SPAN><SPAN class="string token">","</SPAN><SPAN class="punctuation token">,</SPAN> fieldnames <SPAN class="operator token">=</SPAN> headers<SPAN class="punctuation token">)</SPAN>
csvwriter<SPAN class="punctuation token">.</SPAN>writeheader<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
transposeANDwriteCSV<SPAN class="punctuation token">(</SPAN>csvwriter<SPAN class="punctuation token">,</SPAN> data<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">## csvwriter.writerows(zip(*data))</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>