<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Need help with Replace in Field Calculator in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/need-help-with-replace-in-field-calculator/m-p/495476#M38898</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Numa,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not sure if you can use a wildcard with the replace method.&amp;nbsp; The method will read everything within quotes as a string.&amp;nbsp; Try the following instead:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pre-Logic Script Code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def replace(field): &amp;nbsp; if "_" in field: &amp;nbsp;&amp;nbsp;&amp;nbsp; return field.split("_")[0] &amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp; return field&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Under the code block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;replace(&amp;lt;fieldname&amp;gt;)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note:&amp;nbsp; Be sure 'Python' is checked at the top of the Field Calculator&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 30 Jul 2012 12:46:01 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2012-07-30T12:46:01Z</dc:date>
    <item>
      <title>Need help with Replace in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/need-help-with-replace-in-field-calculator/m-p/495475#M38897</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi everyone,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]16464[/ATTACH]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now, after successfully joining my table I would like to get rid of that extension.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I assume that the replace method is the correct one and according to Desktop Help the syntax should be as follows: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;!STATE_NAME!.replace("california", "California")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now, nothing is happening when running my syntax:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;!Gewässerna!.replace("_%", "")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Again, I???m fairly new to using coding in the Field Calculator. I appreciate your help!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Jul 2012 08:43:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/need-help-with-replace-in-field-calculator/m-p/495475#M38897</guid>
      <dc:creator>NumaGremling</dc:creator>
      <dc:date>2012-07-30T08:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with Replace in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/need-help-with-replace-in-field-calculator/m-p/495476#M38898</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Numa,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not sure if you can use a wildcard with the replace method.&amp;nbsp; The method will read everything within quotes as a string.&amp;nbsp; Try the following instead:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pre-Logic Script Code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def replace(field): &amp;nbsp; if "_" in field: &amp;nbsp;&amp;nbsp;&amp;nbsp; return field.split("_")[0] &amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp; return field&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Under the code block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;replace(&amp;lt;fieldname&amp;gt;)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note:&amp;nbsp; Be sure 'Python' is checked at the top of the Field Calculator&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Jul 2012 12:46:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/need-help-with-replace-in-field-calculator/m-p/495476#M38898</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2012-07-30T12:46:01Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with Replace in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/need-help-with-replace-in-field-calculator/m-p/495477#M38899</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This worked perfectly fine! Thank you so much!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I keep looking at the syntax and I just want to make sure I entirely understand what is going on.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Line 3 is where I am not entirely sure. Here is how interpret it;. Please correct me if I am wrong:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;- We are looking for �??_�?�&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- the index [0] is referring to the character itself&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- split will cut off everything from 0 to infinity&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the clarification!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Jul 2012 13:45:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/need-help-with-replace-in-field-calculator/m-p/495477#M38899</guid>
      <dc:creator>NumaGremling</dc:creator>
      <dc:date>2012-07-30T13:45:00Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with Replace in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/need-help-with-replace-in-field-calculator/m-p/495478#M38900</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, you are correct.&amp;nbsp; The split method will return a list of values.&amp;nbsp; For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;string = 'Saur_5'
string.split("_")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;will return:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;['Saur', '5']&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Specifying an index will return the value in the list.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;string.split("_")[0]&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;will return:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;'Saur'&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:47:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/need-help-with-replace-in-field-calculator/m-p/495478#M38900</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T21:47:39Z</dc:date>
    </item>
  </channel>
</rss>

