Select to view content in your preferred language

Need help with Replace in Field Calculator

3893
3
Jump to solution
07-30-2012 01:43 AM
NumaGremling
Deactivated User
Hi everyone,

I am trying to set up a probably very easy expression, but it looks like I???m missing something. Even though I have python background I am new to using it in the Field Calculator (this is actually my first time trying it).

OK, so in order to be able to correctly join a table to a shapefile I had to modify river names. I ended up adding an underscore + a number to specific river segments. The attached screenshot shows what I mean.

[ATTACH=CONFIG]16464[/ATTACH]

Now, after successfully joining my table I would like to get rid of that extension.
I assume that the replace method is the correct one and according to Desktop Help the syntax should be as follows:

!STATE_NAME!.replace("california", "California")


Now, nothing is happening when running my syntax:

!Gewässerna!.replace("_%", "")


Am I not allowed to use a wildcard? Or am I not allowed to replace a string with nothing? Or am I not allowed to replace parts of a string in the first place?

Again, I???m fairly new to using coding in the Field Calculator. I appreciate your help!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Numa,

I'm not sure if you can use a wildcard with the replace method.  The method will read everything within quotes as a string.  Try the following instead:

Pre-Logic Script Code:
def replace(field):   if "_" in field:     return field.split("_")[0]   else:     return field


Under the code block:
replace(<fieldname>)


Note:  Be sure 'Python' is checked at the top of the Field Calculator

View solution in original post

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Numa,

I'm not sure if you can use a wildcard with the replace method.  The method will read everything within quotes as a string.  Try the following instead:

Pre-Logic Script Code:
def replace(field):   if "_" in field:     return field.split("_")[0]   else:     return field


Under the code block:
replace(<fieldname>)


Note:  Be sure 'Python' is checked at the top of the Field Calculator
0 Kudos
NumaGremling
Deactivated User
This worked perfectly fine! Thank you so much!

I keep looking at the syntax and I just want to make sure I entirely understand what is going on.

Line 3 is where I am not entirely sure. Here is how interpret it;. Please correct me if I am wrong:

- We are looking for �??_�?�
- the index [0] is referring to the character itself
- split will cut off everything from 0 to infinity


If that is true, then that means that split only retains what is to the left of the �??split character�??, right? I remember using split in class and I think we used it to chop up a string to use all of its individual parts (i.e. it did something completely different).

Thanks for the clarification!
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Yes, you are correct.  The split method will return a list of values.  For example:

string = 'Saur_5'
string.split("_")


will return:

['Saur', '5']

Specifying an index will return the value in the list.  Ex:

string.split("_")[0]


will return:

'Saur'
0 Kudos