VB or Python script for County label that drops the last word in a field

5513
14
Jump to solution
05-19-2014 02:14 PM
TiaMorita
New Contributor III
I am trying to create labels for a county field name (called "Name") where I drop the last word "county". For example, the Name field has "Los Angeles County" and "Santa Barbara County" and "Ventura County" - but I just want these labels in my map to read: Los Angeles, Santa Barbara, and Ventura. Is there a way to delete the last characters or just drop the whole word County in a script?

Thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Zeke
by
Regular Contributor III
Ok. This works on your sample data.

In Field Calculator...

1. Change parser to python
2. Check Show codeblock
3. Paste the code below into Pre-logic script code
4. in the name = box, enter losemunitype(!name!)

def losemunitype( name):     lsttypes = ['County', 'Parish', 'Borough', 'Municipality', 'Census Area', 'City and Borough']     for losetype in lsttypes:         if losetype in name:             return name.replace(losetype, '')     return name  

View solution in original post

0 Kudos
14 Replies
Zeke
by
Regular Contributor III
In python:!Name!.split()[:-1]
0 Kudos
RaymondHuang
Occasional Contributor
In vbscript: Left([Name], Len([Name])-6)

Basically this prints anything on the Name field and ignoring the last 6 character, which is dropping off the " county"(note the space character).

Hope it helps!

Cheers,
Raymond
0 Kudos
TiaMorita
New Contributor III
In python:!Name!.split()[:-1]


Thanks raymondhuang - I unfortunately get an error message saying "No features found. Could not verify expression."

Any ideas? Thanks again.
0 Kudos
RaymondHuang
Occasional Contributor
Do you mean the VB Script did not work for some records? Can you check if there are any records that doesn't end with " county" or is empty?

Take a screen shot of your attribute table and your field calculator expression for us if you are still facing issues.

Raymond
0 Kudos
JoshuaChisholm
Occasional Contributor III
Thanks raymondhuang - I unfortunately get an error message saying "No features found. Could not verify expression."

Any ideas? Thanks again.


Hello Tia and Raymond,

I think you need to use square brackets[] to reference a field in the label expression. Like this:
[Name].split()[:-1]

But even this code won't work because ArcMap will look for a field called "Name" (which exists) and another field called ":-1" (which does not exist).

I would instead use something like this:
[Name].rstrip(" Country")

This code will remove " Country" from the end of the string iff and only if it's actually there. In other words "Los Angeles" goes to "Los Angeles" and "Los Angeles County" goes to "Los Angeles".

Let me know if that works out!
TiaMorita
New Contributor III
In vbscript: Left([Name], Len([Name])-6)

Basically this prints anything on the Name field and ignoring the last 6 character, which is dropping off the " county"(note the space character).

Hope it helps!

Cheers,
Raymond



Thanks Raymond - Just looking through the County dataset again, I actually see that not all entries end in the word "County". For example I have record names in the "Name" field as:

Aleutians West Census Area
Anchorage Municipality
Denali Borough
Yakutat City and Borough
East Baton Rouge Parish
Los Angeles County
King and Queen County


Essentially, I would like a python or vbscript where if the name field has "Census Area" or "Municipality" or "Borough" or "City and Borough" or "Parish" or "County" -- those names get dropped from the label.

Possible?

Thanks again for the help!!
0 Kudos
JoshuaChisholm
Occasional Contributor III
There are a lot of different ways to do this, but I would use this:
[Name].rstrip("Census Area").rstrip("Municipality").rstrip("Borough").rstrip("City and Borough").rstrip("Parish").rstrip("County").rstrip()


The last .rstrip() gets rid of any spaces at the end of the new string.
0 Kudos
TiaMorita
New Contributor III
There are a lot of different ways to do this, but I would use this:
[Name].rstrip("Census Area").rstrip("Municipality").rstrip("Borough").rstrip("City and Borough").rstrip("Parish").rstrip("County").rstrip()


The last .rstrip() gets rid of any spaces at the end of the new string.


Hi - I wish this was it, but it seems to truncate all my County names. I've attached some sample data to assist and also a jpeg of the result I initially got when I tried the script.


Thanks!
0 Kudos
Zeke
by
Regular Contributor III
Ok. This works on your sample data.

In Field Calculator...

1. Change parser to python
2. Check Show codeblock
3. Paste the code below into Pre-logic script code
4. in the name = box, enter losemunitype(!name!)

def losemunitype( name):     lsttypes = ['County', 'Parish', 'Borough', 'Municipality', 'Census Area', 'City and Borough']     for losetype in lsttypes:         if losetype in name:             return name.replace(losetype, '')     return name  
0 Kudos