Word spacing in labels

1350
10
Jump to solution
05-08-2013 10:34 AM
AmyKlug
Occasional Contributor III
I've got an easy one for you. How do you add a space between field names in a feature class label in python using lblclass.expression? I was using [Field1]& " " &[Field2] and can't find the python method.

Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
I am not sure why you are getting errors.  I just tested this as a script tool and got no errors and it worked as expected.  You do not have to manually set up the expression to be Python.  It is designed to read JavaScript, VB, or Python.  This code worked as a script tool taking 2 field parameters and an input layer:

import arcpy  # Params lyr = arcpy.GetParameterAsText(0) fieldA = arcpy.GetParameterAsText(1) fieldB = arcpy.GetParameterAsText(2)  # Do stuff mxd = arcpy.mapping.MapDocument("CURRENT") layer = arcpy.mapping.ListLayers(mxd, lyr)[0] if layer.supports("LABELCLASSES"):     for lblclass in layer.labelClasses:         lblclass.showClassLabels= True         lblclass.expression= '[%s] + " " + [%s]' %(fieldA,fieldB)         layer.showLabels= True         arcpy.RefreshActiveView()


OR I also got it to work as a multi-value field parameter:

import arcpy  # Params lyr = arcpy.GetParameterAsText(0) fields = arcpy.GetParameterAsText(1).split(';')  # Do stuff mxd = arcpy.mapping.MapDocument("CURRENT") layer = arcpy.mapping.ListLayers(mxd, lyr)[0] if layer.supports("LABELCLASSES"):     for lblclass in layer.labelClasses:         lblclass.showClassLabels= True         lblclass.expression= '[%s] + " " + [%s]' %(fields[0],fields[1])         layer.showLabels= True         arcpy.RefreshActiveView()


EDIT: I just fixed a small quote error

View solution in original post

0 Kudos
10 Replies
by Anonymous User
Not applicable
Easy indeed:

def FindLabel( [Field1], [Field2] ):
  label = " ".join([str(i) for i in [[Field1], [Field2]] if i != None])
  return label


OR

def FindLabel( [Field1], [Field2] ):
  label = str([Field1]) + " " + str([Field2])
  return label
0 Kudos
AmyKlug
Occasional Contributor III
Can I use with lblclass.expression?

>>> import arcpy
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> layer = arcpy.mapping.ListLayers(mxd, "PolyLineFeatureClass")[0]
>>> if layer.supports("LABELCLASSES"):
...     for lblclass in layer.labelClasses:
...         lblclass.showClassLabels= True
...         lblclass.expression= "[Field1]& " " &[Field2]"
...         layer.showLabels= True
...         arcpy.RefreshActiveView()



the " " are not recognized and added to the label expression box which is vb.
0 Kudos
JennB
by
New Contributor III
Why not go ahead and give it a try? 🙂

lblclass.expression = str([Field1]) + " " + str([Field2])


Not actually able to test Caleb's idea but there's no harm in trying that.
0 Kudos
AmyKlug
Occasional Contributor III
Why not go ahead and give it a try? 🙂

lblclass.expression = str([Field1]) + " " + str([Field2])


Not actually able to test Caleb's idea but there's no harm in trying that.


I tried that earlier but it didn't work. Python does not read " ". I even tried putting quotations and brackets.

I found a way to add a new line by inserting "vbNewLine" but I have not found a way to add a white space yet

lblclass.expression = '"%s" + [Field1] + vbNewLine + [Field2] + "%s"' % ("<CLR red='255'><FNT size = '10'>", "</FNT></CLR>")
0 Kudos
by Anonymous User
Not applicable
I tried that earlier but it didn't work. Python does not read " ". I even tried putting quotations and brackets.

I found a way to add a new line by inserting "vbNewLine" but I have not found a way to add a white space yet


Python does read a space as " ".  Do you want to use a space or new line? In Python, a new line is "\n". 

# for a space
lblclass.expression= "%s %s" %([Field1],[Field2])


new line:
lblclass.expression= "%s\n%s" %([Field1],[Field2])


If you have any Null values you may want to use the .join() method instead with a list comprehension because using str() with a None value will be a string of 'None' rather than blank.

>>> t = None  # Nonetype is how Python sees Null values
>>> a = 'text'
>>> '%s %s' %(t,a)
'None text'
>>> '{0} {1}'.format(t,a)
'None text'
>>> label = ' '.join([str(i)for i in [t,a] if i != None])
>>> print label
text
>>> t = 'string'
>>> label = ' '.join([str(i)for i in [t,a] if i != None])
>>> label
'string text'


So for your label class expression:

lblClass.expression =  ' '.join([str(i)for i in [ [Field1],[Field2]] if i != None])


Make sure to substitute Field1 and Field2 with your actual field names.
0 Kudos
AmyKlug
Occasional Contributor III
Thanks!

When I try to add a space with:

lblclass.expression= "%s %s" %([Field1], [Field2])

I get "Name Error: name 'Field1' is not defined
0 Kudos
by Anonymous User
Not applicable
Sorry, I forgot this has to be supplied as a string...This is why I should test things before posting...

lblclass.expression= " '%s %s' %([Field1], [Field2])"
0 Kudos
AmyKlug
Occasional Contributor III
Sorry, I forgot this has to be supplied as a string...This is why I should test things before posting... 

lblclass.expression= " '%s %s' %([Field1], [Field2])"


I guess I should tell you I'm making a script tool.

The problem with this is when it's added from the python window to the label expression box in the feature class, the label expression box is set to vbscript so you have to go in to the feature class properties and manually change the parser to "python". I tried adding a "%s %s" into the middle of the lblclass.expression:

lblclass.expression= '"%s" + [Field1] + "%s %s" + [Field2] + "%s"' % ("<CLR red='230'>", "</CLR>")


but that didn't work "not enough arguments for the format string". I might need something like VBAddSpace.

maybe i will just use a "-" to separate the fields. This should be so easy 🙂
0 Kudos
AmyKlug
Occasional Contributor III
Found it:

lblclass.expression= '"%s" + [Field1] + Chr(32) + [Field2] + "%s"' % ("<BOL><CLR red='255' blue='0' green='152'><FNT size = '8'>", "</FNT></CLR></BOL>")


Edit: Had posted vbKeySpace, but that didn't work. Chr(32) definitely works
0 Kudos