Python Label Expression

6482
15
10-01-2013 12:36 PM
BartonGriesenauer
New Contributor II
I am trying to create one label expression that has two label options.  The first option will stack the parcel number on top of the lot number and the other option would stack the parcel number on top of deeded acreage.  The [LOTNUMBER] field, [PARCEL] field, are text fields the [DEEDACRES] is a double.  I have no python experience, did not think this expression would be this difficult to come up with.  I would appreciate any help.


def FindLabel ( [PARCEL] , [LOTNUMBER] , [DEEDACRES] 😞
  if long([LOTNUMBER]) >1:
    return "<CLR yellow='255'>" +  [PARCEL] + "</CLR>" + '\n' + [LOTNUMBER]
  else:
    return "<CLR yellow='255'>" +  [PARCEL] + "</CLR>" + '\n' + [DEEDACRES]
Tags (2)
15 Replies
T__WayneWhitley
Frequent Contributor
..at the very least, don't you have to convert the double to string?  Use 'str'
0 Kudos
T__WayneWhitley
Frequent Contributor
...and don't forget your 'closing' formatting tags, this works fine for experimental purposes for me - could have just changed the label font to yellow instead of using text formatting tags, unless you have other label classes or something.

The below was just fiddling around to mimic the same design you had, where my ID field is numeric:
def FindLabel ( [NAME], [ID] , [RECHAR]  ):
     if long( [RECHAR].split('-')[0] )<=80000:
         return "<CLR yellow='255'>"+ [RECHAR] + '\n' +  [NAME] + "</CLR>" 
     elif long( [RECHAR].split('-')[0]) > 80000:
          return "<CLR yellow='255'>"+ [NAME] + '\n' + str( [ID] ) + "</CLR>"


Hope that helps - also when you post code (another example of text tags), it will display correctly if you select (highlight) your code section and use the hashtag symbol (#)...which simply should place the tags in...
 goes at the beginning, I think you get my drift, just reversed them here to negate the effect?



EDIT:  Please don't apply the '.split('-')[0]' on your code -- I did that only on my data to coerce text into a val I can use 'long' on.

Enjoy,
Wayne
0 Kudos
SiranErysian
New Contributor

I need assistance with labeling also. I am new to Python and I may have posted this but I am going to try to get your assistance on this since I've been reading your answers to this string from Barton.

I am trying to label contours that are designated as "major" which are the rounded number in the values fields--300, 350, 400, etc. and not label all of the contours. I've been in the Python mode, advanced checked in the label expression dialog box but how do I Find a label in one field and label it some value from another field? Is this possible?

0 Kudos
MattSayler
Occasional Contributor II
To elaborate on what Wayne is saying, fields that are one of the numerical data types (long integer, short integer, double, float) can't be concatenated with strings (it's akin to asking the interpreter what number the letter "a" + 1 evaluates to). Use the built in str() method to convert numbers to strings and then it will be able to put them together (i.e. "a" + str(1) == "a" + "1" == "a1").

So if the data types for [LOTNUMBER] and [DEEDACRES] are numerical, something like this:
def FindLabel ( [PARCEL] , [LOTNUMBER] , [DEEDACRES] ):
  if long([LOTNUMBER]) >1:
    return "<CLR yellow='255'>" +  [PARCEL] + "</CLR>" + '\n' + str([LOTNUMBER])
  else:
    return "<CLR yellow='255'>" +  [PARCEL] + "</CLR>" + '\n' + str([DEEDACRES])
0 Kudos
T__WayneWhitley
Frequent Contributor
ha ha, yes, very well put, Matt!  Excellent.
0 Kudos
BartonGriesenauer
New Contributor II
First off, thank you for all your help.  I am not even a novice at Python, so I don't think I stated my initial question very well.  I want to label the parcels that are lots one way and the metes & bounds parcels another way.  I want to label the parcels that are lots, the lot field is not null, as parcel stacked on top of lot number.  I want to label the parcels that are not lots, the lot field is null, as parcel stacked on top of deeded acreage.  I tried both your solutions and still the parcels that are not lots are not getting labelled at all.  My PARCEL and LOTNUMBER fields are text fields.  DEEDACRES is a double field.  I apologize for any confusion.
0 Kudos
MattSayler
Occasional Contributor II
How about:
def FindLabel ( [PARCEL] , [LOTNUMBER] , [DEEDACRES] ):
  if [LOTNUMBER].strip() == None:
    return "<CLR yellow='255'>" + [PARCEL] + "</CLR>\n" + str([DEEDACRES])
  else:
    return "<CLR yellow='255'>" + [PARCEL] + "</CLR>\n" + [LOTNUMBER]
0 Kudos
BartonGriesenauer
New Contributor II
Still not labeling parcels with null lot number attributes.  I have attached a screen capture.
0 Kudos
MatthewDobson
Occasional Contributor
Won't "not" work? I haven't corrected for your specific example, but what about something like:

def FindLabel ( [contractorName] ):
  if not [contractorName]:
    a = "Apples"
  else:
    a = [contractorName]
  return a
0 Kudos