I am attempting to add fields to a table and then populate the fields with text values (Yes/No). The text values are based on whether or not a species was found in a specific block. If the species was found in a block then the field > 0 and if no species was found in a block then field = 0.
I am having issues with my select by attribute where clause. Specifically, I am trying the use the field name text from my ListFields function in the sql statement (where_clause= "{}" >= "0" .format(arcpy.AddFieldDelimiters(blyr, field4)). However, I keep getting failed to execute (SelectByAttribute).
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="comment token"># To allow overwriting the outputs change the overwrite option to true.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token"># Purpose is to generate a table with presence absence data for fisheries blocks falling inside/outside of species range </SPAN>
<SPAN class="comment token"># ################################################################################</SPAN>
<SPAN class="comment token"># [1] Script creates needed variables for for loop with conditional statement #</SPAN>
<SPAN class="comment token"># [2] for loop uses list and conditional uses counter (to skip first four fields)#</SPAN>
<SPAN class="comment token"># [3] Script adds duplicate (text) fields to attribute table with shortened name #</SPAN>
<SPAN class="comment token"># [4] Adds yes/no values to new fields #</SPAN>
<SPAN class="comment token"># ################################################################################</SPAN>
<SPAN class="comment token"># [1] Identify fisheries block shapefile joined with commercial catch information and associated layer file (for make feature layer tool below_)</SPAN>
blocks <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Users\WMcenery\Documents\GIS_Projects\SpeciesRange\Workspace\scrap\FB_Ground_MLR.shp"</SPAN>
blyr <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Users\WMcenery\Documents\GIS_Projects\SpeciesRange\Workspace\scrap\FB_Ground_MLR.lyr"</SPAN>
local <SPAN class="operator token">=</SPAN>r<SPAN class="string token">"C:\Users\WMcenery\Documents\GIS_Projects\SpeciesRange\Workspace\scrap"</SPAN>
lyr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>in_features <SPAN class="operator token">=</SPAN> blocks<SPAN class="punctuation token">,</SPAN> out_layer <SPAN class="operator token">=</SPAN> blyr<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># [1] Generate list for each field in blocks shapefile and set counter = 0 </SPAN>
fields <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>blocks<SPAN class="punctuation token">)</SPAN>
x <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN>
<SPAN class="comment token">#Line 28</SPAN>
<SPAN class="comment token"># [2-4] For loop creates duplicate text fields in block shapefile and populates the new fiels with yes/no text</SPAN>
<SPAN class="keyword token">for</SPAN> field <SPAN class="keyword token">in</SPAN> fields<SPAN class="punctuation token">:</SPAN>
field4 <SPAN class="operator token">=</SPAN> field<SPAN class="punctuation token">.</SPAN>name
f4 <SPAN class="operator token">=</SPAN> field4<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">8</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"X"</SPAN>
x <SPAN class="operator token">=</SPAN> x <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">if</SPAN> x <SPAN class="operator token">>=</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#35</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">,</SPAN> f4<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"TEXT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">,</SPAN> selection_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN> <SPAN class="string token">"{}"</SPAN> <SPAN class="operator token">>=</SPAN> <SPAN class="string token">"0"</SPAN> <SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>AddFieldDelimiters<SPAN class="punctuation token">(</SPAN>blyr<SPAN class="punctuation token">,</SPAN> field4<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CalculateField_management<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">,</SPAN> f4<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Yes'</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">,</SPAN> selection_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN> <SPAN class="string token">"""{} = 0"""</SPAN> <SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>AddFieldDelimiters<SPAN class="punctuation token">(</SPAN>blyr<SPAN class="punctuation token">,</SPAN> field4<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CalculateField_management<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">,</SPAN> f4<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'No'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</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>