[GIS] How to trim strings in Field Calculator

arcgis-desktopfield-calculatorpythonstring

I am currently working on a County plat book and am in the process of prepping the "Owner_Nm" field that will serve as my annotation layer. I am trying to whittle down the owner names, based on parcel size, to either full name (easily done) to first initial + full last name or just initials. The format of the names is currently something like this "John H Smith" or "John H Smith & Lucy L Smith" for example. I know next to nothing about python in field calculator so I am hoping to get some help from this community. INRE: how to write my python code in field calculator to return, dependent on parcel size, either (using the above examples) "J H Smith" or "J H S" or "J H Smith & L L Smith" or "J H S & L L S". I have nothing to offer as to the code I've tried as it has mostly returned gibberish….Thanks in advance.

Best Answer

Well, I agree with one of the comments above about using Maplex if that will work for you. If it doesn't though, what you could do is use the field calculator as you suggest. For the all initials (J H S & L L S) You would want to do something like the following:

Code Block / Pre-Logic Script Code:

def nameCalc(inName):
    outName = ""
    namesList = inName.split("&")
    for name in namesList:
        outName = outName + " &"
        nameL = name.split()
        for word in nameL:
            outName = outName + " " + word[0]
    outName = outName.strip(" &")
    return outName

and then in the expression you would put nameCalc(!Owner_Nm!)

You could select the parcels you want just initials for and then run that. However, if you want to do them all at once and you have an area field or such as that that you want to use to automate the process (ex: all parcels larger than or equal to 5 acres get full name, between 5 and 1 acres get abreviated, and smaller than 1 gets just initials), then you could do something like the following in the code block:

def nameCalc(inName,area):
    outName = ""
    if area < 1:
        namesList = inName.split("&")
        for name in namesList:
            outName = outName + " &"
            nameL = name.split()
            for word in nameL:
                outName = outName + " " + word[0]
        outName = outName.strip(" &")
    if area >= 1 and area < 5:
        namesList = inName.split("&")
        for name in namesList:
            outName = outName + " &"
            nameL = name.split()
            for word in nameL:
                if word != nameL[-1]:
                    outName = outName + " " + word[0]
                else:
                    outName = outName + " " + word
    if area  >= 5:
        outName = inName
    outName = outName.strip(" &")
    return outName

and then in the expression you would put nameCalc(!Owner_Nm!,!Acres!) PLEASE NOTE THOUGH: if you do that, you would A) Have to replace the 1, 1, 5, & 5 with the appropriate values for your data and what you want and B) you would have to replace !Acres! in the expression with whatever area/size field you wanted to use and that field would have to be a numeric field for the code above to run correctly.

I know that's a lot of code, and it may be a bit to work your way through, but it should return each of the 3 desired results and will correctly handle both single names and double names separated by &

I hope that helps, and if you have questions, feel free to comment and if I can help, I will try to.

Related Question