I have a featureclass of street names where all of the names are in uppercase. I can set a label class using an expression STN begins with "MC%", but I want python to label the streets starting with MC to convert the label to proper case, for example, MCDAVID would be McDavid. Does anyone have a suggestion for using python to do this? I've already tried using [STN].title(), but that yields Mcdavid. Thanks for the suggestions.
Solved! Go to Solution.
Your little script is almost correct. I had to change nam[0:1] to nam[0:2], because the "c" got skipped and yielded MDavid. The following works, and yields McDavid. Thanks so much!!!
def FindLabel ( [STN] ):
nam = [STN].lower()
nam = nam.replace("mc", "Mc")
nam = nam[0:2] + nam[2].capitalize() + nam[3:]
return nam
You could always do a replace?
STN.replace("MC", "Mc")
That partially works.
STN.replace("MC", "Mc")
returns McDAVID. What I want is McDavid. Thanks for your help. I'll keep trying.
Ohhh it's all in caps. Something like this should work?
I'm not putting in an if statement because you have it handled by the Label class query.
nam = STN.lower()
nam = nam.replace("mc", "Mc")
nam = nam[0:2] + nam[2].capitalize() + nam[3:]
return nam
Edited to change my slicing since I mixed it up a bit.
Your little script is almost correct. I had to change nam[0:1] to nam[0:2], because the "c" got skipped and yielded MDavid. The following works, and yields McDavid. Thanks so much!!!
def FindLabel ( [STN] ):
nam = [STN].lower()
nam = nam.replace("mc", "Mc")
nam = nam[0:2] + nam[2].capitalize() + nam[3:]
return nam
That's what I get for posting without testing lol. Glad it works.
Probably a better way, but his is how I've handled that in the past.
I have a dictionary of the names that get whacked out when title casing and substitute the value when has a matching key.
subDict = {
"MCMURRAY ST": "McMurray St",
"MCMURRAY AVE": "McMurray Ave",
"MCINTOSH CT": "McIntosh Ct",
"MCEWAN DR": "McEwan Dr",
"MCPHERSON AVE": "McPherson Ave",
"MCCLELLAN ST": "McClellen St",
"BY-PASS": "Bypass Hwy SR 240",
"O'CONNOR ST":"O'Connor St",
"MCMURRAY": "McMurray",
"MCINTOSH": "McIntosh",
"MCEWAN": "McEwan",
"MCPHERSON": "McPherson",
"MCCLELLAN": "McClellen",
"O'CONNOR":"O'Connor"
}
def FindLabel ( [Street] ):
st = [Street]
label = [Street].title()
if st in subDict.keys():
label = subDict[st]
return label
R_