Select to view content in your preferred language

Python Label Expression; Using multiple if's to label fields > 0

7653
13
Jump to solution
03-02-2015 08:39 AM
MCAGIS
by
Emerging Contributor

I've written this code, and the expression checks out, but it only labels the first field. Can someone tell me why it doesn't run through the rest of the if's to create a stacked label with field >0?

 

def FindLabel ( [Light_Numb], [IN_FDN] , [IN_35_FT_METAL_POST] , [IN_35_5] , [IN_40_5] , [IN_6_FT_ARM] , [IN_12_FT_ARM] , [IN_18_FT_ARM] , [IN_X_ARM] , [IN_LED_87W_OH] , [IN_LED_101W_OH] , [IN_LED_110W_OH] , [IN_LED_260W_OH] , [IN_LED_87W_UG] , [IN_LED_101W_UG] , [IN_LED_110W_UG] , [IN_LED_260W_UG] , [IN_PC] , [IN_SC] , [CONC_WORK_REQ_SQ_FT]  😞

 

 

try:

    if [Light_Numb] > 0:

        return [Light_Numb]

    '\n'

    if [In_FDN] > 0:

        return 'In FDN', [IN_FDN]

    '\n'

    if [IN_35_FT_METAL_POST] > 0:

        return 'IN 35 FT METAL POST', [IN_35_FT_METAL_POST]

    '\n'

    if [IN_35_5] > 0:

        return 'IN 35-5', [IN_35_5]

    '\n'

    if [IN_40_5] > 0:

        return 'IN 40-5', [IN_40_5]

    '\n'

    if [IN_6_FT_ARM] > 0:

        return 'IN 6 FT ARM', [IN_6_FT_ARM]

    '\n'

    if [IN_12_FT_ARM] > 0:

        return 'IN 12 FT ARM', [IN_12_FT_ARM]

    '\n'

    if [IN_18_FT_ARM] > 0:

        return 'IN_18 FT ARM', [IN_18_FT_ARM]

    '\n'

    if [IN_X_ARM] > 0:

        return 'IN X ARM', [IN_X_ARM]

    '\n'

    if [IN_LED_87W_OH] > 0:

        return 'IN LED 87W', [IN_LED_87W_OH]

    '\n'

    if [IN_LED_87W_UG] > 0:

        return 'IN LED 87W', [IN_LED_87W_UG]

    '\n'

    if [IN_LED_101W_OH] > 0:

        return 'IN LED 101W', [IN_LED_101W_OH]

    '\n'

    if [IN_LED_101W_UG] > 0:

        return 'IN LED 101W', [IN_LED_101W_UG]

    '\n'

    if [IN_LED_110W_OH] > 0:

        return 'IN LED 110W', [IN_LED_110W_OH]

    '\n'

    if [IN_LED_110W_UG] > 0:

        return 'IN LED 110W', [IN_LED_110W_UG]

    '\n'

    if [IN_LED_260W_OH] > 0:

        return 'IN LED 260W', [IN_LED_260W_OH]

    '\n'

    if [IN_LED_260W_UG] > 0:

        return 'IN LED 260W', [IN_LED_260W_UG]

    '\n'

    if [IN_PC] > 0:

        return 'IN PC', [IN_PC]

    '\n'

    if [IN_SC] > 0:

        return 'IN SC', [IN_SC]

    '\n'

    if [CONC_WORK_REQ_SQ_FT] > 0:

        return 'CONC WORK REQ SQFT', [CONC_WORK_REQ_SQ_FT]

except:

return 'None'

0 Kudos
13 Replies
JoshuaBixby
MVP Esteemed Contributor

Can you post some specific error messages?  Also, what about a table/feature class with a subset of your data?  I tested the code on some examples I created without issue, but obviously my guessing on your data structure wasn't right.

0 Kudos
DavidFox
Occasional Contributor

Maybe the attached table will help.

And of course the error message in ArcGIS isn't selectable to copy and paste ...

label_code_error.JPG

0 Kudos
DarrenWiens2
MVP Alum

You must (?) enclose your field names in brackets (as you did long before) in the label expression FindLabel function :

def FindLabel ( [stand_code], [Avg_TPA_Pine], [Avg_TPA_Cyp], [Avg_TPA_Hdwd] ): 
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I will save any explanations until after I see whether this works for you:

def FindLabel ( [stand_code], [Avg_TPA_Pi], [Avg_TPA_Cy], [Avg_TPA_Hd] ):
    stand_code = [stand_code]
    Avg_TPA_Pine = [Avg_TPA_Pi]
    Avg_TPA_Cyp = [Avg_TPA_Cy]
    Avg_TPA_Hdwd = [Avg_TPA_Hd]
    label = ""  
    try:  
          if int(Avg_TPA_Pine) > 0:  
              label += "TPA P: " + Avg_TPA_Pine + " / "  
          if int(Avg_TPA_Cyp) > 0:  
              label += "TPA C: " + Avg_TPA_Cyp + " / "  
          if int(Avg_TPA_Hdwd) > 0:  
              label += "TPA H: " + Avg_TPA_Hdwd  
          return stand_code + '\n'+ label  
    except:  
          return  stand_code 
0 Kudos