Select to view content in your preferred language

Simple Python label expression FAIL!

285
3
Jump to solution
03-17-2025 09:44 AM
KevinBell1
Regular Contributor

In the python window, this works as expected:

n = 1313.87
type(n)
<class 'float'>
str(n).split('.')[1]
'87'

HOWEVER, trying to do the same to label features fails:

KevinBell1_0-1742229637894.png

Note that PtZ is a Double, and the label expression str([PtZ]) labels just fine.

What am I doing wrong here???

Pro version 3.4

0 Kudos
2 Solutions

Accepted Solutions
CodyPatterson
MVP Regular Contributor

Hey @KevinBell1 

It looks like when using the label expressions, it's placing a "return" in front of the expression. This normally means you will need to create a function for this, with set return values, something like this here:

def FindLabel([PtZ]):
    val = str([PtZ])
    parts = val.split('.')
    if len(parts) > 1:
        return parts[1]
    else:
        return ''

Let me know if this ends up working for you!

Cody

View solution in original post

JoshuaBixby
MVP Esteemed Contributor

It can be simplified a bit more even.  The [PtZ] doesn't have to be stored in a separate variable, it is already a variable:

def FindLabel([PtZ]):
    return [PtZ].split('.')[1]

View solution in original post

3 Replies
CodyPatterson
MVP Regular Contributor

Hey @KevinBell1 

It looks like when using the label expressions, it's placing a "return" in front of the expression. This normally means you will need to create a function for this, with set return values, something like this here:

def FindLabel([PtZ]):
    val = str([PtZ])
    parts = val.split('.')
    if len(parts) > 1:
        return parts[1]
    else:
        return ''

Let me know if this ends up working for you!

Cody

JoshuaBixby
MVP Esteemed Contributor

It can be simplified a bit more even.  The [PtZ] doesn't have to be stored in a separate variable, it is already a variable:

def FindLabel([PtZ]):
    return [PtZ].split('.')[1]
KevinBell1
Regular Contributor

Great!  That works, but is some weird ESRI stuff going on since you can't split a float/double!  

0 Kudos