So I have a massive amount of data I need to get into a text element, so I wrote a python script for this.
textValue += "<BOL>" + row[0] + ": </BOL>" + row[1] + " - " + row[2] + '\r\n'
It works great.
001: John Doe - 12345
002: Jane Smith - 67890
The problem happens when a "&" symbol is introduced.
<BOL>001:</BOL> John Doe - 12345
<BOL>002:</BOL> Jeff & Jane Smith - 67890
I get that this is a formatting rule issue, but is there away I can get around this. I guess I could write a script to replace all “&” with “and” but I prefer not to. I appreciate any suggestions.
Have you tried using ascii symbol value ... chr(38)?
Sorry for the confusion, it is not because I want the symbol, it is just that the symbol is already there in a lot of the attributes that I am trying to write to a text element.
So what happens when you encounter the "&"? Is it a run-time error or a formatting error? Since the character is already in the data the only thing I can think of is replacing it.
It still runs fine, but the results of my list in the text element are <BOL>002:</BOL> Jeff & Jane Smith - 67890 instead of 002: Jeff & Jane Smith - 67890. If there just 1 row that has the "&" the whole list will look like <BOL>002:</BOL> Jeff & Jane Smith - 67890. It ignores the bold formatting altogether. I think you are right I might have to just replace them, was hoping someone ran into this type a thing before and had a work around. Thanks for the reply.
Instead of replacing with and, you can replace with it with "&", which is read by the text formatter as an ampersand. I made a duplicate field in my attribute table and replaced my "&" with "&" and it labelled properly off the copy field.
Thanks for the tip! This is exactly what I will do, if I do not find a way to do it without replacing.
further testing, you don't need to make a new field.
I replaced the value in the label expression then added it to the expression I wanted returned and it worked perfectly.
def FindLabel ([Test]): label = [Test].replace("&" , "&") return "<BOL>" + label + "</BOL>"
This worked for a text field that had an ampersand on it. John & Jim displayed correctly on a point with this expression. Also I explored python string formatting and nothing worked on that front.