Solved! Go to Solution.
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()
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()
def FindLabel( [Field1], [Field2] ): label = " ".join([str(i) for i in [[Field1], [Field2]] if i != None]) return label
def FindLabel( [Field1], [Field2] ): label = str([Field1]) + " " + str([Field2]) return label
>>> 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()
lblclass.expression = str([Field1]) + " " + str([Field2])
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.
lblclass.expression = '"%s" + [Field1] + vbNewLine + [Field2] + "%s"' % ("<CLR red='255'><FNT size = '10'>", "</FNT></CLR>")
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
# for a space lblclass.expression= "%s %s" %([Field1],[Field2])
lblclass.expression= "%s\n%s" %([Field1],[Field2])
>>> 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'
lblClass.expression = ' '.join([str(i)for i in [ [Field1],[Field2]] if i != None])
lblclass.expression= "%s %s" %([Field1], [Field2])I get "Name Error: name 'Field1' is not defined
lblclass.expression= " '%s %s' %([Field1], [Field2])"
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])"
lblclass.expression= '"%s" + [Field1] + "%s %s" + [Field2] + "%s"' % ("<CLR red='230'>", "</CLR>")
lblclass.expression= '"%s" + [Field1] + Chr(32) + [Field2] + "%s"' % ("<BOL><CLR red='255' blue='0' green='152'><FNT size = '8'>", "</FNT></CLR></BOL>")