Select to view content in your preferred language

Difficulty using List.Field in for loop to make selection and populate attribute table

516
1
02-26-2020 02:35 PM
by Anonymous User
Not applicable

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).

import arcpy
from arcpy import env

# To allow overwriting the outputs change the overwrite option to true.
arcpy.env.overwriteOutput = True

# Purpose is to generate a table with presence absence data for fisheries blocks falling inside/outside of species range 

# ################################################################################
# [1] Script creates needed variables for for loop with conditional statement    #
# [2] for loop uses list and conditional uses counter (to skip first four fields)#
# [3] Script adds duplicate (text) fields to attribute table with shortened name #
# [4] Adds yes/no values to new fields                                           #
# ################################################################################

# [1] Identify fisheries block shapefile joined with commercial catch information and associated layer file (for make feature layer tool below_)
blocks = r"C:\Users\WMcenery\Documents\GIS_Projects\SpeciesRange\Workspace\scrap\FB_Ground_MLR.shp"
blyr   = r"C:\Users\WMcenery\Documents\GIS_Projects\SpeciesRange\Workspace\scrap\FB_Ground_MLR.lyr"

local =r"C:\Users\WMcenery\Documents\GIS_Projects\SpeciesRange\Workspace\scrap"
lyr = arcpy.MakeFeatureLayer_management(in_features = blocks, out_layer = blyr)
# [1] Generate list for each field in blocks shapefile and set counter = 0 
fields = arcpy.ListFields(blocks) 



x = 0
#Line 28
# [2-4] For loop creates duplicate text fields in block shapefile and populates the new fiels with yes/no text
for field in fields:
    field4 = field.name
    f4 = field4[0:8] + "X"
    x = x + 1
    if x >= 5:
        #35
        arcpy.AddField_management(lyr, f4, "TEXT")                  
        arcpy.SelectLayerByAttribute_management(lyr, selection_type="NEW_SELECTION", where_clause= "{}" >= "0" .format(arcpy.AddFieldDelimiters(blyr, field4)))
        arcpy.CalculateField_management(lyr, f4, 'Yes')
        arcpy.SelectLayerByAttribute_management(lyr, selection_type="NEW_SELECTION", where_clause= """{} = 0""" .format(arcpy.AddFieldDelimiters(blyr, field4)))
        arcpy.CalculateField_management(lyr, f4, 'No')
    else:
        print


    ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Reply
JoshuaBixby
MVP Esteemed Contributor

Try:

where_clause= "{} >= 0".format(arcpy.AddFieldDelimiters(blyr, field4))
0 Kudos