Select to view content in your preferred language

Word spacing in labels

2064
10
Jump to solution
05-08-2013 10:34 AM
AmyKlug
Frequent Contributor
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
10 Replies
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
0 Kudos