Select to view content in your preferred language

Changing Field Characters

3767
7
Jump to solution
10-10-2014 12:39 PM
MichelleCouden1
Deactivated User

I have a field with some stuff in it I don't want in it. For Example: It is the highway route name with - KG at the end. Example : IH0045 - KG. I want to remove the -KG. How do I do this??

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

is your field named RID?  Did you toggle on the python processor at the top of the field calculator dialog?  See the attached images.  Image one is the before when I added the field.  Image 2 is when I did the field calculation.   As for the dataframe...it is a long story

The exact expression I used was  !RID!.replace(" - KG","")

Pumpkin1.pngPumpkin2.png

View solution in original post

0 Kudos
7 Replies
TimWitt2
MVP Alum

Michelle,

this might answer your question.

https://community.esri.com/thread/42674

0 Kudos
DanPatterson_Retired
MVP Emeritus

Some options in Python  interpreter

>>> "IH0045 - KG".replace("- KG","")

'IH0045 '       #Not great leaves the trailing space after ...45

>>> "IH0045 - KG".replace(" - KG","")

'IH0045'        #Better but you have to remember the extra space

>>> "IH0045 - KG".split(" ")[0]

'IH0045'        #OK allows you to get the first bit

>>>

now in the field calculator it would look like this

!YourField!.replace("- KG","")

!YourField!.replace(" - KG","")

!YourField!.split(" ")[0]

where !YourField! is obvious but enclosed in exclamation marks.  All comments above depend upon the data structure you have provided...other options will depend upon any variants you can now think of

0 Kudos
MichelleCouden1
Deactivated User

OK, I used the second one of your codes.

!RID!.replace(" - KG","")

It said it ran and finished in 3 seconds but it did not change the field???

0 Kudos
DanPatterson_Retired
MVP Emeritus

is your field named RID?  Did you toggle on the python processor at the top of the field calculator dialog?  See the attached images.  Image one is the before when I added the field.  Image 2 is when I did the field calculation.   As for the dataframe...it is a long story

The exact expression I used was  !RID!.replace(" - KG","")

Pumpkin1.pngPumpkin2.png

0 Kudos
MichelleCouden1
Deactivated User

Thank You bunches! I see where I messed up. My -KG had no spaces so it was thinking it worked the code when the code I provided was wrong.  Python. Gotta remember it wants spaces correct! Thanks again.

0 Kudos
Zeke
by
Honored Contributor

There are a couple ways, depending on your data. Is the format consistent? In Field Calculator (parser set to Python), one way is this, where <yourfield> is your field name:

<yourfield>.split()[0]

This will return the part of the string before the first space, e.g. IH0045. split() takes the whole string and breaks it into a list using a space to break the elements of the string as a separator. [0] references the first element of the list. You could give split() an argument and break on that if a space isn't what you want. For example, split('4') would give you the list ['IH00', '5 - KG'}. The 4 in this example, and the space in the first, are not part of the list.

If your data is not in a consistent format, or what you want removed is not consistent, this may not give you what you want. That would be a more complex operation that would depend on the data.

0 Kudos
StevenGraf1
Frequent Contributor

Find and Replace would work just fine if you didn't want to mess with VB or Python

0 Kudos