I've got a number of different labeling scripts I use and they all at least contain one NAME type field I display. On the rare occasion, I have names that contain the "&" (such as "Rough & Ready").
I know the ampersand is a special character and when I use it in my script it starts displaying the other fileds parameters in the output text.
Here is an example of one of my simple labeling codes in Python:
def FindLabel([Name_1],[Width_1]):
str_list = []
if [Name_1] not in (None, "<NULL>","<Null>", "", "0"):
str_list.append([Name_1])
if [Width_1] not in (None, "<NULL>","<Null>", "", "0", " ","Varies"):
Width_1_str = '<ITA>Width = {}\'</ITA>'.format("<ITA>" + [Width_1] + "</ITA>")
str_list.append(Width_1_str)
label_str = '\n'.join(str_list)
return(label_str)
I have found an example using VB and somehow incorporating "&","&" into the script, but I have played around with adding variations of that in my code, but I can't seem to get it to work without errors.
Any ideas how to simply allow & into my Name_1 field in this example?
Thanks
Solved! Go to Solution.
Thank for the input on Italics. Turns out, my Pro uses Tahoma font by default and apperantly the <ITA> tag doesn't work with it. I changed to Arial and is working.
As far as the failing with & and only NAME, that is because there is no text tag in play when that is the only populated field so the & replacement isn't needed and will show the & in this case.
Easy fix, I just added an UN-bold <_BOL> to the name and Route string. That way, no matter what, at least one text formatting tag will be used, regardless of the number of populated fields.
This seems to do what you are after (I hope anyway :))
def FindLabel([NAME],[ALT_NAME],[ROUTE],[WIDTH]):
NAME1 = str([NAME]).strip() # assign variable to string with whitespace stripped
ALT_NAME1 = str([ALT_NAME]).strip() # assign variable to string with whitespace stripped
ROUTE1 = str([ROUTE]).strip() # assign variable to string with whitespace stripped
WIDTH1 = str([WIDTH]).strip() # assign variable to string with whitespace stripped
str_list = []
if [NAME]: #Make sure is not empty since or can append 'None' to label
if '&' in NAME1:
NAME1=NAME1.replace("&","&")
if NAME1 not in ("<NULL>", "", "0"): # since already string, test if in(values)
name_str = f"<_BOL>{NAME1}</_BOL>"
str_list.append(name_str) # append to list if not in the filter list
if [ALT_NAME]:
if '&' in ALT_NAME1:
ALT_NAME1=ALT_NAME1.replace("&","&")
if ALT_NAME1 not in ("<NULL>", "", "0"):
altname_str = f"<CLR red = '255'><ITA><FNT size = '6'>({ALT_NAME1})</FNT></ITA></CLR>" # I changed to f'strings as easier to visualize than .format
str_list.append(altname_str)
if [ROUTE]:
if ROUTE1.upper() not in ("<NULL>", "", "0", "VARIES"): # Cast compare variable to upper case to test for "VARIES"
route_str = f"<_BOL>RT# {ROUTE1}</_BOL>" # does not sound like you have much control over data
str_list.append(route_str) # so casting to upper case will catch other spellings/Case
if [WIDTH]:
if WIDTH1.upper() not in ("<NULL>", "", "0", "VARIES"):
width_str = f"<ITA>Width = {WIDTH1}'</ITA>"
str_list.append(width_str)
label_str = '\n'.join(str_list)
return(label_str)
R_
Well that seemed to solve that issue, but creates a new one now.
I can often have just the WIDTH supplied without a NAME. This code won't display it if there is no NAME given.
Also, if I add in a Route and not ALT_NAME, it duplicates the NAME field.
This is one interesting things you're trying to solve here ha...
Whoops, left a relic append in there. Try the above code now that I have deleted line 28 (str_list.append(name_str)).
R_
Yes, it now seems to work. I've just made a few adjustments to only bold the NAME if needed and never the other ones and it works as I originally wanted it.
Thanks for that tutorial and guess I put your mind to work a bit too...