Use inline variable for a field name in Select Layer by Attribute ----HELP!!!!

1946
6
02-16-2021 12:03 AM
HussainMalik1
New Contributor III

Hi, 

@XanderBakker , @DavidPike , @SMehta 

How can I use an Inline Variable as a field name in Select Layer by Attribute in code block.  

I want to get the field name from multivalued variable table rather than from Attribute Table.  In Select Layer by Attribute   Year and %VariableFieldName%  value in "where clause" comes from string Var2 as shown in the Image.

 

def x(Var2):
    for i in Var2.split(";"):
        a,b=shlex.split(i)
        with arcpy.EnvManager(scratchWorkspace=r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb", workspace=r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb"):
lyr = arcpy.management.SelectLayerByAttribute(r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb\IDEAL_SHAPE_TimeSeries_Feb14_V1", "NEW_SELECTION", f"Year = {a} And %VariableFieldName% >0, None)

Data type : 
Long

 

 HussainMalik1_0-1613461576272.png

I will really appreciate your help

Thanks 

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

This needs to be fixed...

f"Year = {a} And %VariableFieldName%

you are missing a " and, And is and 

try something along the lines of

'"Year" = {} and "{}" > 0'.format(a, v)

where 'a' is the year and v is the variable

Also packing your code into a one-liner isn't very useful

Code formatting ... the Community Version - GeoNet, The Esri Community

split this up

s_ws = r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb"
ws = r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb"
with arcpy.EnvManager(scratchWorkspace=s_ws,workspace=ws):
    # etc

 to make it readable 


... sort of retired...
HussainMalik1
New Contributor III

Thanks @DanPatterson  for the help .

I am getting the following Syntax Error message

ERROR 000539: File "<string>", line 6
SyntaxError: f-string: empty expression not allowed
Failed to execute (Calculate Value Multiple Fields).

Please do check attached image for detail geoprocessing message

Thanks 

Hussain

0 Kudos
DanPatterson
MVP Esteemed Contributor

A screengrab of the code isn't going to help.

It also appears that you didn't take my suggestion of making the existing code more readable .

I can't tell if you fixed the error I pointed out in your expression


... sort of retired...
0 Kudos
HussainMalik1
New Contributor III

@DanPatterson 

I changed it according to your advice and it does looks better though. 

 

Expression: x(("%VT%")
import shlex
s_ws = r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb"
ws = r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb"
tbl= "IDEAL_SHAPE_TimeSeries_Feb14_V1"
def x(VT):
    for i in VT.split(";"):
        a,b,c=shlex.split(i)
        with arcpy.EnvManager(scratchWorkspace=s_ws, workspace=ws):
            lyr = arcpy.management.SelectLayerByAttribute(tbl, "NEW_SELECTION", f'"Year" = {} and "{}">0'.format(a,b) , None)
            x = arcpy.management.GetCount(lyr)
 ERROR 000539:   File "<string>", line 9
SyntaxError: f-string: empty expression not allowed
 Failed to execute (Calculate Value Multiple Fields).

 I am also trying to figure out the next part of this code where I have to calculate a new field and use the values from same field that I select in the code above (string "b" in this example ). Correct me if I am wrong if SeachCursor would be a good approach for that. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

throw in a print statement, it apparently doesn't see a or b


... sort of retired...
HussainMalik1
New Contributor III

So I have changed the SelectLayerByAttribute and put the values (a and b) from string VT1  in curly brackets. where {a} = Year field and {b} = !ELECUSE_Large_Schools! field in attribute table. 

Code below is a snippet from model builder Python in Arcpro. 

Now this runs with no error message but it only selects the last part of the  VT1 string. 

How can i make it run for the whole VT1 variable?   

import shlex
s_ws = r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb"
ws = r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb"
tbl= "IDEAL_SHAPE_TimeSeries_Feb14_V1"
def x(VT1):
    for i in VT1.split(";"):
        a,b,c= shlex.split(i)
        with arcpy.EnvManager(scratchWorkspace=s_ws, workspace=ws):
            lyr= arcpy.management.SelectLayerByAttribute(tbl, "NEW_SELECTION", f'"Year" = {a} and "{b}">0'.format(a,b),None)
            x= arcpy.management.GetCount(lyr)

def Model5(VT1=[[2020, "ELECUSE_Large_Schools", 0.2], [2040, "ELECUSE_Large_Schools", 0]]):  # Model 4

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = False


    # Process: Calculate Value (Calculate Value) ()
    if VT1:
        Value = x("VT1")

if __name__ == '__main__':
    # Global Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb", workspace=r"D:\GBT_R_Drive\GBT_GIS\ICEMIgdb\ICEM\BldgEmission.gdb"):
        Model5(*argv[1:])

 Thanks 

 

0 Kudos