<?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: Multiple replacements with Field Calculations using Python in Petroleum Questions</title>
    <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140577#M95</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Welllll, you're welcome to explore the &lt;A href="https://docs.python.org/2/library/re.html" rel="nofollow noopener noreferrer" target="_blank"&gt;regex library&lt;/A&gt;​, but I've only done so cursorily. Easier, you could make a dictionary with the full name paired with the shortened name, then compare the first two characters against that. Note that you'll have issues in cases like Arkansas and Arizona:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;dict = {'California':'ca', 'Florida':'fl'} # and so on
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;for&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; k,v &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;in&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; dict.iteritems(): &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# loop through key/value pairs&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;for&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; abbrev &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;in&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; v: &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# loop through the abbreviation list&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;if&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; search[:2] == abbrev: &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# compare&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; first two characters&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;return&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; k &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# return the state&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;return&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; search &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# else return original&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN style="background-color: inherit; color: black; font-weight: inherit; font-size: 9pt !important; font-style: inherit;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 07:45:21 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2021-12-11T07:45:21Z</dc:date>
    <item>
      <title>Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140566#M84</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="margin-top: auto; margin-bottom: 2.4pt;"&gt; &lt;SPAN lang="EN" style="font-size: 13.5pt; font-family: 'Verdana',sans-serif; color: #103e4e;"&gt;I am able to make replacements one at a time following these instructions:&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-top: auto; margin-bottom: 2.4pt;"&gt;&lt;SPAN lang="EN" style="font-size: 13.5pt; font-family: 'Verdana',sans-serif; color: #103e4e;"&gt;Simple calculations&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-top: auto; margin-bottom: 2.4pt;"&gt;&lt;STRONG&gt;&lt;SPAN lang="EN" style="font-size: 11.5pt; font-family: 'Verdana',sans-serif; color: #af7209;"&gt;Simple string examples&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P style="margin-top: 6.0pt; margin-bottom: 6.0pt; margin-left: 41.25pt;"&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Verdana',sans-serif;"&gt;Strings are supported by a series of Python string functions, including &lt;/SPAN&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Courier New';"&gt;capitalize&lt;/SPAN&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Verdana',sans-serif;"&gt;, &lt;/SPAN&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Courier New';"&gt;rstrip&lt;/SPAN&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Verdana',sans-serif;"&gt;, and &lt;/SPAN&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Courier New';"&gt;replace&lt;/SPAN&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Verdana',sans-serif;"&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-top: 6.0pt; margin-bottom: 6.0pt; margin-left: 41.25pt;"&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Verdana',sans-serif;"&gt;Replace any occurrences of "california" with "California" found in the field &lt;/SPAN&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Courier New';"&gt;STATE_NAME&lt;/SPAN&gt;&lt;SPAN lang="EN" style="font-size: 9.5pt; font-family: 'Verdana',sans-serif;"&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .05in; margin-left: 45.0pt; background: #EEEEEE;"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;!STATE_NAME!.replace("california", "California")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 14.0pt; font-family: 'Verdana',sans-serif;"&gt;How do I make several replacements at a time? For example, how do I change “california” and “cal” and “CA” and “Cal” to “California” and “florida” and “fla” and “FL” and “Fla” to “Florida” without having to create a new field for STATE NAME for each individual replacement?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 14.0pt; font-family: 'Verdana',sans-serif;"&gt;Thanks, Len Springer&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 13:18:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140566#M84</guid>
      <dc:creator>LeonardSpringer</dc:creator>
      <dc:date>2015-08-26T13:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140567#M85</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just an idea - I guess part of this could come down to whether one could use wildcards in Python.&amp;nbsp; I don't know the exact answer, but can outline the concept.&amp;nbsp; For example, if you know your data will only include state names expressed in various abbreviations, something like:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;!STATE_NAME!.replace("ca#", "California")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;where # is the wildcard allowing unlimited characters after it (whatever the appropriate Python wildcard would be).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm pretty sure the Python Guru's here on GeoNet would know.&amp;nbsp; I'm curious to see what folks come up with.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chris Donohue, GISP&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 15:08:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140567#M85</guid>
      <dc:creator>ChrisDonohue__GISP</dc:creator>
      <dc:date>2015-08-26T15:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140568#M86</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A quick search revealed this thread regarding Python wildcards.&amp;nbsp; Apparently in Python * is the one for 'one or more characters'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;They also mention other ways to do the search that may be usable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/11427138/python-wildcard-search-in-string" title="http://stackoverflow.com/questions/11427138/python-wildcard-search-in-string"&gt;http://stackoverflow.com/questions/11427138/python-wildcard-search-in-string&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chris Donohue, GISP&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 15:14:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140568#M86</guid>
      <dc:creator>ChrisDonohue__GISP</dc:creator>
      <dc:date>2015-08-26T15:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140569#M87</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hmmm, maybe it is time to call in some Python experts:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/3100"&gt;Xander Bakker&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/3116"&gt;Dan Patterson&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/19932"&gt;Darren Wiens&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chris Donohue, GISP&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 18:39:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140569#M87</guid>
      <dc:creator>ChrisDonohue__GISP</dc:creator>
      <dc:date>2015-08-26T18:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140570#M88</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I used a function to do something like this and it should be adaptable for what you are needing.&amp;nbsp; I had a table with mixed upper and lower cases no's and Nones, that I all wanted "No".&amp;nbsp; Also replaced all "yes" with "Yes".&amp;nbsp; The function allows you to use loops and other python tools, instead of a single line of code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you know all the exact strings you need replaced, you can put them in lists, otherwise wildcards could be used.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Python Parser, click Show Code Block&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Pre-Logic Script Code&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def Rename(field):
&amp;nbsp; type1_replace = [ "no" , "None"]
&amp;nbsp; type2_replace = "yes"
&amp;nbsp; for n in type1_replace:
&amp;nbsp;&amp;nbsp;&amp;nbsp; field = field.replace(n , "No")
&amp;nbsp; field = field.replace(type2_replace , "Yes")
&amp;nbsp; return field&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;New Field = Rename(!YourFieldHere!)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edit: Ah heck, I'll post a relevant to you example.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def Rename(field):
&amp;nbsp; type1_replace = ["california" ,&amp;nbsp; "CA" , "cal" ]
&amp;nbsp; type2_replace = [ "florida" , "FL" , "fl"]
&amp;nbsp; for n in type1_replace:
&amp;nbsp;&amp;nbsp;&amp;nbsp; field = field.replace(n , "California")
&amp;nbsp; for n2 in type2_replace:
&amp;nbsp;&amp;nbsp;&amp;nbsp; field = field.replace(n2 , "Florida")
&amp;nbsp; return field&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:45:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140570#M88</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2021-12-11T07:45:13Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140571#M89</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Also, why would you need to create a new field each time?&amp;nbsp; You could run the field calculator over and over again on the existing field if you wanted.&amp;nbsp; Calculate Field can be done on an existing field or a new empty field.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 19:56:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140571#M89</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2015-08-26T19:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140572#M90</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the code example, Ian.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In regards to the new fields, I think Leonard means making a new line for each replacement, not the literal making a new field.&amp;nbsp; For example, having to do this to account for all the possible combinations:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;!STATE_NAME!.replace("california", "California")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;!STATE_NAME!.replace("ca", "California")&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;!STATE_NAME!.replace("cal", "California")&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;!STATE_NAME!.replace("cali", "California")&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;&lt;SPAN lang="EN" style="font-size: 10.0pt; font-family: 'Courier New';"&gt;etc....&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chris Donohue, GISP&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 20:22:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140572#M90</guid>
      <dc:creator>ChrisDonohue__GISP</dc:creator>
      <dc:date>2015-08-26T20:22:11Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140573#M91</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can do this using a dictionary, where the key is the state name, and the value is a list of possible matches:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def customReplace(search):
&amp;nbsp;&amp;nbsp;&amp;nbsp; dict = {'California':['c','ca','cal'], 'Florida': ['f','flor','fla']} # build your custom dictionary
&amp;nbsp;&amp;nbsp;&amp;nbsp; for k,v in dict.iteritems(): # loop through key/value pairs
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for abbrev in v: # loop through the abbreviation list
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if search == abbrev: # compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return k # return the state
&amp;nbsp;&amp;nbsp;&amp;nbsp; return search # else return original&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:45:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140573#M91</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T07:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140574#M92</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Darren - is there also a way to do wildcards instead of a dictionary?&amp;nbsp; Like if the values to be evaluated can only be state names, so any combination starting with "Wy...." is going to be "Wyoming".&amp;nbsp; This would save having to type out every possible combination people use to abbreviate Wyoming (and other states).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chris Donohue, GISP&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 20:59:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140574#M92</guid>
      <dc:creator>ChrisDonohue__GISP</dc:creator>
      <dc:date>2015-08-26T20:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140575#M93</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;no fancy function but the logic is the same as dictionaries...what is easiest to convert your data to???&amp;nbsp; If your table has missing keys...I have opted to leave out the flawed entry.&amp;nbsp; You can alter this behaviour.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; data = ['CA', 'Fla', 'Cal', 'FL', 'Ontario']
&amp;gt;&amp;gt;&amp;gt; innies = [[["california", "cal","CA","Cal"],"California"],[["florida","fla","FL","Fla"],"Florida"]]
&amp;gt;&amp;gt;&amp;gt; outies=[]
&amp;gt;&amp;gt;&amp;gt; for j in data:
...&amp;nbsp; for i in innies:
...&amp;nbsp; if j in i[0]:
...&amp;nbsp;&amp;nbsp;&amp;nbsp; outies.append(i[1])
...&amp;nbsp;&amp;nbsp; 
&amp;gt;&amp;gt;&amp;gt; outies
['California', 'Florida', 'California', 'Florida']
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:45:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140575#M93</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T07:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140576#M94</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you, Ian. That works for me.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 21:09:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140576#M94</guid>
      <dc:creator>LeonardSpringer</dc:creator>
      <dc:date>2015-08-26T21:09:39Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140577#M95</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Welllll, you're welcome to explore the &lt;A href="https://docs.python.org/2/library/re.html" rel="nofollow noopener noreferrer" target="_blank"&gt;regex library&lt;/A&gt;​, but I've only done so cursorily. Easier, you could make a dictionary with the full name paired with the shortened name, then compare the first two characters against that. Note that you'll have issues in cases like Arkansas and Arizona:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;dict = {'California':'ca', 'Florida':'fl'} # and so on
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;for&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; k,v &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;in&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; dict.iteritems(): &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# loop through key/value pairs&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;for&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; abbrev &lt;/SPAN&gt;&lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;in&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; v: &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# loop through the abbreviation list&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;if&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; search[:2] == abbrev: &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# compare&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; first two characters&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;return&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; k &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# return the state&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword" style="font-weight: inherit; font-style: inherit; color: #006699; font-size: 9pt !important; background-color: inherit;"&gt;return&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; search &lt;/SPAN&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# else return original&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN style="background-color: inherit; color: black; font-weight: inherit; font-size: 9pt !important; font-style: inherit;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:45:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140577#M95</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T07:45:21Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140578#M96</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'll have to have a word with Congress about our state names.&amp;nbsp; Maybe if we renamed them to be more distinctly unique, think of all the money saved and confusion avoided....&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chris Donohue, GISP&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 21:31:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140578#M96</guid>
      <dc:creator>ChrisDonohue__GISP</dc:creator>
      <dc:date>2015-08-26T21:31:30Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140579#M97</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Definitely a more pythonic solution and in the case of having more states would be significantly improved over my version. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I should really use dictionaries more often, but since I only use python with arcgis I haven't used them alot(exception of storing data from cursors with them).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 21:47:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140579#M97</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2015-08-26T21:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140580#M98</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There are numerous ways to address this issue, some of which have already been raised here.&amp;nbsp; A Google search for "&lt;A href="http://www.google.com/search?q=python+replace+multiple+strings" rel="nofollow noopener noreferrer" target="_blank"&gt;python replace multiple strings&lt;/A&gt;" will yield even more ideas, some of them with fairly in-depth discussions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to stick with using the Field Calculator, then you are going to have to use&lt;A href="http://desktop.arcgis.com/en/desktop/latest/manage-data/tables/calculate-field-examples.htm#ESRI_SECTION1_DF13F7EE4AF345CAAA46C1CFA2F7BFE1" rel="nofollow noopener noreferrer" target="_blank"&gt; code blocks&lt;/A&gt;.&amp;nbsp; Another option would be to use ArcPy cursors, the &lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/what-is-the-data-access-module-.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Data Access &lt;/A&gt;ones are the newest and best.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;An approach I have used in the past that is simple, and seems to perform well enough with my data sets, is a variation of a suggestion from a Stack Exchange/Overflow post:&amp;nbsp; &lt;A href="http://stackoverflow.com/questions/15175142/how-can-i-do-multiple-substitutions-using-regex-in-python" rel="nofollow noopener noreferrer" target="_blank"&gt;How can I do multiple substitutions using regex in python&lt;/A&gt;?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import re
repls = {
&amp;nbsp;&amp;nbsp;&amp;nbsp; 'california': 'California',
&amp;nbsp;&amp;nbsp;&amp;nbsp; 'cal': 'California',
&amp;nbsp;&amp;nbsp;&amp;nbsp; 'ca': 'California',
&amp;nbsp;&amp;nbsp;&amp;nbsp; 'florida':'Florida',
&amp;nbsp;&amp;nbsp;&amp;nbsp; 'fl':'Florida'
}
def multiple_replace(text):
&amp;nbsp;&amp;nbsp;&amp;nbsp; regex = re.compile("(%s\\b)" % "&lt;A&gt;\\b|".join(map(re.escape&lt;/A&gt;, repls.keys())),re.I)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return regex.sub(lambda mo: repls[mo.string[mo.start():mo.end()].lower()], text)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The above code goes in the code block, and the field box would be &lt;SPAN style="font-family: courier new,courier;"&gt;multiple_replace(!State!).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A couple of notes.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;You will need some kind of mapping between what you want to replace and what you are replacing it with.&amp;nbsp; In this example, a dictionary is being used but other Python data structures could be used as well.&lt;/LI&gt;&lt;LI&gt;The posted code works on upper or lower case, but the dictionary keys need to be lowercase or you will get a Key Error potentially.&lt;/LI&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:45:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140580#M98</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T07:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple replacements with Field Calculations using Python</title>
      <link>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140581#M99</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Regular expressions are extremely worthwhile, especially since they are a language in and of themselves so your knowledge can be transferred beyond Python.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 22:18:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/petroleum-questions/multiple-replacements-with-field-calculations/m-p/140581#M99</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-08-26T22:18:36Z</dc:date>
    </item>
  </channel>
</rss>

