<?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 Python scripting in Field Calculator: String to Integer w/ Conditional Statement in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/python-scripting-in-field-calculator-string-to/m-p/185078#M6259</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Good day,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've been having some issues writing a python script for a Field Calculation. The code I've written: &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;yyyy = !DATE_LCL![4:]:

dd = !DATE_LCL![5,6]:

if !DATE_LCL![7,9] = 'Jan':
&amp;nbsp;&amp;nbsp; mm = 01
elif !DATE_LCL![7,9] = 'Feb':
&amp;nbsp;&amp;nbsp; mm = 02
else:
&amp;nbsp;&amp;nbsp; mm = 99
end if&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Field:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;yyyy + mm + dd&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This script is desigend to take a string field (DATE_LCL) of a standardized date (ex. Thu 15Jul2010) and convert it to the following integer format: yyyymmdd. The current code is only coverting from the initial format to the new format while retaining the string data type. Eventually, I'll be converting it into a short int. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;While running the code, I get the 000989 error code which is an invalid syntax. While I have no doubt that my syntax is invalid, I'm sure my code has some more fundamental issues as well. Any help?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 06 Aug 2010 13:12:55 GMT</pubDate>
    <dc:creator>BradleyCabe</dc:creator>
    <dc:date>2010-08-06T13:12:55Z</dc:date>
    <item>
      <title>Python scripting in Field Calculator: String to Integer w/ Conditional Statement</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-scripting-in-field-calculator-string-to/m-p/185078#M6259</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Good day,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've been having some issues writing a python script for a Field Calculation. The code I've written: &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;yyyy = !DATE_LCL![4:]:

dd = !DATE_LCL![5,6]:

if !DATE_LCL![7,9] = 'Jan':
&amp;nbsp;&amp;nbsp; mm = 01
elif !DATE_LCL![7,9] = 'Feb':
&amp;nbsp;&amp;nbsp; mm = 02
else:
&amp;nbsp;&amp;nbsp; mm = 99
end if&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Field:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;yyyy + mm + dd&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This script is desigend to take a string field (DATE_LCL) of a standardized date (ex. Thu 15Jul2010) and convert it to the following integer format: yyyymmdd. The current code is only coverting from the initial format to the new format while retaining the string data type. Eventually, I'll be converting it into a short int. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;While running the code, I get the 000989 error code which is an invalid syntax. While I have no doubt that my syntax is invalid, I'm sure my code has some more fundamental issues as well. Any help?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Aug 2010 13:12:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-scripting-in-field-calculator-string-to/m-p/185078#M6259</guid>
      <dc:creator>BradleyCabe</dc:creator>
      <dc:date>2010-08-06T13:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: Python scripting in Field Calculator: String to Integer w/ Conditional Statement</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-scripting-in-field-calculator-string-to/m-p/185079#M6260</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I believe this is what you would want:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;convertDate(!DATE_LCL!)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def convertDate(sdate):
&amp;nbsp;&amp;nbsp;&amp;nbsp; yyyy = sdate[-4:]
&amp;nbsp;&amp;nbsp;&amp;nbsp; dd = sdate[4:6]
&amp;nbsp;&amp;nbsp;&amp;nbsp; if sdate[6:9] == 'Jan':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mm = "01"
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif sdate[6:9] == 'Feb':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mm = "02"
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mm = "99"
&amp;nbsp;&amp;nbsp;&amp;nbsp; return yyyy + dd + mm&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Basically, you need to create a Python function in the code block (this is the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;def&lt;/SPAN&gt;&lt;SPAN&gt;), and call that function from the expression.&amp;nbsp; The use of ! for delimiters is something that you use only in the expression--it is just a way of passing a field value down to the function.&amp;nbsp; In the function, you assign the incoming value a variable name, in this case &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;sdate&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It easy to test these directly in Python.&amp;nbsp; If you created a new Python script, add this function above in the top, and add an additional line, something like...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;newdate = convertDate("Thu 15Jul2010")
print newdate&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;... this will give you a very quick idea if your code is working (and if not, will be easy to debug).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Dave&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:23:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-scripting-in-field-calculator-string-to/m-p/185079#M6260</guid>
      <dc:creator>DavidWynne</dc:creator>
      <dc:date>2021-12-11T09:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: Python scripting in Field Calculator: String to Integer w/ Conditional Statement</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-scripting-in-field-calculator-string-to/m-p/185080#M6261</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;FANTASTIC!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you. Not only did you solve this issue, you also taught me something about python! &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Aug 2010 15:46:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-scripting-in-field-calculator-string-to/m-p/185080#M6261</guid>
      <dc:creator>BradleyCabe</dc:creator>
      <dc:date>2010-08-06T15:46:00Z</dc:date>
    </item>
  </channel>
</rss>

