Select to view content in your preferred language

Using if in Label Expression - Python

9228
12
Jump to solution
01-03-2013 06:28 AM
MikeBly
Regular Contributor
Hi All,

I am trying to setup a label expression using python parser in ArcGIS 10.1. Simply, this expression looks at parcel attributes, and if the parcel size is >= 41, label it, otherwise, do not label. Also, if parcel meets requirement, split the string (ownername) at the comma and stack label, so last name on top, first name on bottom.

If the string does not contain a comma, stack at first space in string.

I have done part of this using VBscript parser, to the point where it splits at comma and stacks. I would like to move this over to a working python maintain consistency is scripting language that we use. I cannot get python to even look at the size of the parcel.

A working example of the VB is below:

Function FindLabel ( [ANAME], [ACRES] )
if ([ACRES] >= 41) Then
  myArray = Split ( [ANAME] , "," )
  strLabel = myArray (0)
  strname = Split ( [ANAME] , " " )
   For i = 1 To UBound (myArray)
   strLabel = strLabel & "," & vbNewLine & myArray (i)  
   FindLabel = strLabel
Next
End if
End Function


Thanks,

Mike
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MikeBly
Regular Contributor
Wayne,

Thank you for that last reply, it got me on the right track where I wanted to be. I have the code to where I want it at this point. I will add more to it. Now the trick is to keep only the first letter of each string after the comma create last name and initials.

Thanks for the help, how does one put the code into a separate window in this forum (code window)?

Mike

def FindLabel ( [ANAME], [ACRE_FLT]  😞
   if float ( [ACRE_FLT] ) >=41: 
    S = [ANAME] 
    if ', ' in S:
               S = S.replace ( ', ', ',\n' )
    elif ' ' in S:
               S = S.split (' ')
               S = S[0] + '\n' + S[1] + '\n' + S[2]
    return S

View solution in original post

0 Kudos
12 Replies
MikeBly
Regular Contributor
I was able to get it started in Python,

It filters by parcel size, splits and stacks, however I have used the .replace function. Doing this, it drops the comma, I would like to keep this and the end of the first word because it is a name label.

Any ideas on how to do this, is there a split at "," type of line i can put in?

Simple python script below. My issue was that python, as far as I know will not work with type double, so I converted to float.

Thanks.

def FindLabel ( [ANAME], [ACRE_FLT]  😞
   if float ( [ACRE_FLT] ) >=41: 
    S = [ANAME]
    S = S.replace ( ', ',  '\n' )
    return S
0 Kudos
by Anonymous User
Not applicable
Yes, you can use the split() function:

def FindLabel ( [ANAME], [ACRE_FLT] ):
    if float ( [ACRE_FLT] ) >=41:
        S = [ANAME]
        S = S.split(',')
    return S 


you can also index the split:

string = 'value1,value2'

#string.split(',')[0] will return 'value1'
#string.split(',')[1] will return 'value2'
0 Kudos
T__WayneWhitley
Honored Contributor
Caleb is right, and just to put a finish on it to include what you said you wanted, a stacked label including a comma at the end of the 1st line --- then in Caleb's function, after the line:

S = S.split(',')

...insert:

S = S[0] + ',\n' + S[1]


I think that will form the complete string you want for the label.

-Wayne
0 Kudos
MikeBly
Regular Contributor
Thanks guys,

When I try the split function, it does not throw any errors, however it does not label anything or return anything, just blank. No labels.

When I use the S = S[0] + ',\n' + S[1], I get the first and second letters of the string stacked with a comma. So the comma return but drops all characters after the [1].

Thoughts on this?

Mike
0 Kudos
T__WayneWhitley
Honored Contributor
You still need the 'split' line - it sounds like the comma character was missing if you applied the split function -- if you use the split function searching for a character to split by that doesn't exist in the string, it simply returns the whole string (it doesn't split it)...so what you can do is if you're missing the ',' character, is conditionally test the length of the string, len(S), which will be 1 if it doesn't split.  You'd have to handle that differently.

So, to be clear, you should test (note that I do not have 10.1 installed, so you have to test it - the following is Caleb's code as I was talking about with a single line added after the 'split' function):
def FindLabel ( [ANAME], [ACRE_FLT] ):
    if float ( [ACRE_FLT] ) >=41:
        S = [ANAME]
        S = S.split(',')
        S = S[0] + ',\n' + S[1]
    return S
0 Kudos
T__WayneWhitley
Honored Contributor
Not to confuse things, but I noticed you were originally using a sample from the 10.1 webhelp, the line:

S = S.replace ( ', ', '\n' )


So what this actually does is simply replace the comma and following single space ', ' with a linefeed character; or plainly put, takes out the comma (and space) and makes the string 2 lines where the comma was removed.

Then, all you need to do is add the comma back in, using your original function posted (your 2nd post) - notice the subtle difference:

S = S.replace ( ', ', ',\n' )
0 Kudos
MikeBly
Regular Contributor
Wayne,

Thank you, not sure what i did the first time, this does keep the , now. What it does not do is return the strings that do not contain the character (,), these parcels remain unlabelled.

This I will have to work on.

Thanks,

MIke
0 Kudos
T__WayneWhitley
Honored Contributor
...so this is the conditional statement you'd have to enter, for example you can do something like test for a comma, or test for something else and treat accordingly, if this do this, elif that do that (sorry for the brevity) --- what do you have to test for, a space?

EDIT:
Then, to illustrate, something like this (test it, make sure to get the indention correct):
def FindLabel ( [ANAME], [ACRE_FLT] ):
    if float ( [ACRE_FLT] ) >=41: 
          S = [ANAME]
          if ', ' in S:
               S = S.replace ( ', ', ',\n' )
          elif ' ' in S:
               S = S.replace (' ', ',\n')
          else:
               # do something else if another test is necessary, but for now simply pass
               pass
    return S 
0 Kudos
MikeBly
Regular Contributor
Wayne,

Thank you for that last reply, it got me on the right track where I wanted to be. I have the code to where I want it at this point. I will add more to it. Now the trick is to keep only the first letter of each string after the comma create last name and initials.

Thanks for the help, how does one put the code into a separate window in this forum (code window)?

Mike

def FindLabel ( [ANAME], [ACRE_FLT]  😞
   if float ( [ACRE_FLT] ) >=41: 
    S = [ANAME] 
    if ', ' in S:
               S = S.replace ( ', ', ',\n' )
    elif ' ' in S:
               S = S.split (' ')
               S = S[0] + '\n' + S[1] + '\n' + S[2]
    return S
0 Kudos