Select to view content in your preferred language

Multiple If-Else Python Code for Symbology

1227
8
07-16-2019 12:01 PM
by Anonymous User
Not applicable

I am trying to adjust symbology using Python for a project with multiple fields that require if-else statements. The expression is below. No errors are being thrown, but there is no output. Please help if you can.

This is what I would like it to end up looking like:

  MW-1

B: 20,000

T: 5,000

E: 9,0000

X:40,0000

Code:

def FindLabel ( [Name],[Benzene1],[Toluene1],[Ethylbenzene1],[Xylenes1] 😞

    out = ""

   

    out += "<UND>"+"<BOL>"+str([Name])+"</BOL>"+"</UND>"+"\n"

   

    if float([Benzene1]) > 20000.0:

        #bold

        out += "<BOL>"+"B: "+ str([Benzene1]) +"</BOL>"+"\n"

    else:

        #normal

        out += "B: "+ str([Benzene1]) +"\n"

       

        return out

   

    if float([Toluene1]) > 10000.0:

        #bold

        out += "<BOL>"+"T: "+ str([Toluene1]) +"</BOL>"+"\n"

    else:

        #normal

        out += "T: "+ str([Toluene1]) +"\n"

        return out

   

    if float([Ethylbenzene1]) > 15000.0:

        #bold

        out += "<BOL>"+"E: "+ str([Ethylbenzene1]) +"</BOL>"+"\n"

    else:

        #normal

        out += "E: "+ str([Ethylbenzene1]) +"\n"

        return out

   

    if float([Xylenes1]) > 12000.0:

        #bold

        out += "<BOL>"+"X: "+ str([Xylenes1]) +"</BOL>"+"\n"

    else:

        #normal

        out += "X: "+ str([Xylenes1]) +"\n"

        return out

Tags (2)
0 Kudos
8 Replies
LanceCole
MVP Regular Contributor

Try This...

def FindLabel ( [Name],[Benzene1],[Toluene1],[Ethylbenzene1],[Xylenes1] ):
    out = ""
    out += "<UND><BOL>"+str([Name])+"</BOL></UND>\n"
    
    if float([Benzene1]) > 20000.0:
        #bold
        out += "<BOL>B: "+ str([Benzene1]) +"</BOL>\n"
    else:
        #normal
        out += "B: "+ str([Benzene1]) +"\n"
        
    if float([Toluene1]) > 10000.0:
        #bold
        out += "<BOL>T: "+ str([Toluene1]) +"</BOL>\n"
    else:
        #normal
        out += "T: "+ str([Toluene1]) +"\n"
    
    if float([Ethylbenzene1]) > 15000.0:
        #bold
        out += "<BOL>E: "+ str([Ethylbenzene1]) +"</BOL>\n"
    else:
        #normal
        out += "E: "+ str([Ethylbenzene1]) +"\n"
    
    if float([Xylenes1]) > 12000.0:
        #bold
        out += "<BOL>X: "+ str([Xylenes1]) +"</BOL>\n"
    else:
        #normal
        out += "X: "+ str([Xylenes1]) +"\n"
    
    return out‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
by Anonymous User
Not applicable

I just tried that, it still didn't give me any errors, but it is not displaying anything still.

0 Kudos
LanceCole
MVP Regular Contributor

Brian,

I did have one typo in the previous reply but you should have got an error when you clicked Verify.  I have edited it since.

Take a few steps back and start with the following and build from there.  Also, watch your variable types.  I assume [Name] is a string so you do not need str([Name]).  Depending upon your system you may need "\r\n" for a new line not just "\n".

def FindLabel([Name],[Benzene1],[Toluene1],[Ethylbenzene1],[Xylenes1]):
  out = ""
  out += "<UND><BLD>" + [Name] + "</BOL></UND>\n"
  return out‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
by Anonymous User
Not applicable

Good point about the string, I'll change that and watch out for that one. So I edited my expression and noticed the code worked for 5 out of 20 of the wells. The other wells simply did not display any information. I am thinking this may be a bug regarding ArcGIS Pro. The 'return out' was not working, as it ended the expression. The way you had it with the 'return out' at the end worked though. I submitted the expression to ESRI for review regarding the potential bug. Thank you for your help Lance, I'll update the post once I hear back from them to close the question. 

0 Kudos
LanceCole
MVP Regular Contributor

Brian,

My comment on str() statements also applies to your float() statements.  If the valve passed to the function is a float type you do not need to include a float() statement.  You only need to use float() or str() when you are changing the type.  Not knowing your data types, I could not make revisions but made an assumption on [Name] being a string.

You are using both in your code for example float([Benzene1]) and str([Benzene1]). If [Benzene1] is a string you will need to use the float() when needed  Likewise, if [Benzene1] is a float you will need to use the str() when needed.  

Another item to check is does your data have any NULL data or blank fields.  Your code does not handle these values and would result in the data not displaying.  If you want to upload your data in a shapefile or table, I can take a look at it.  I was thinking you were working in desktop not pro, not that that makes a too big a difference.

0 Kudos
by Anonymous User
Not applicable

Interestingly enough, the only string is [Name], all others are a double. But for some reason ArcPro was reading them as a string. I'm unsure why that is. I'll upload my data if you want to take a look.

0 Kudos
by Anonymous User
Not applicable

Is there a way to upload to this post specifically? I only see how to publish my data to geonet separately.

0 Kudos
LanceCole
MVP Regular Contributor

Brian Mainor‌,

1) Click Reply

2) At the top, click "Use advanced editor"

advance editor

3) Once you do this there will be an option at the bottom to attach a file.

attach

0 Kudos