<?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: Converting Field Calculator Pre-Logic Script to pyscript in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290575#M22508</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The simplest way to get the proper syntax is to use your calculation in model builder then export as python script. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 01 Jul 2016 12:00:26 GMT</pubDate>
    <dc:creator>WesMiller</dc:creator>
    <dc:date>2016-07-01T12:00:26Z</dc:date>
    <item>
      <title>Converting Field Calculator Pre-Logic Script to pyscript</title>
      <link>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290573#M22506</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've been struggling to emulate a simple Field Calculator function as pyscript. When I try, I get parsing errors.&lt;/P&gt;&lt;P&gt;For example, how should this simple function (when manually applied in the Field Calculator tool) look when written up as pyscript?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In Pre-Logic Script Code:&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt; &lt;SPAN style="font-family: 'Courier New'; color: #c55a11;"&gt;def LookUp ( a, b ) :&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: #c55a11;"&gt;&amp;nbsp; if ( a == 0 ) :&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: #c55a11;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; newval = 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: #c55a11;"&gt;&amp;nbsp; else :&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: #c55a11;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; newval = b&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: #c55a11;"&gt;&amp;nbsp; return newval&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif;"&gt;In FIELD = &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: #c55a11;"&gt;LookUp ( !MAX_RANK!, !Shape_Area! )&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: #303030;"&gt;Any advice will be much appreciated.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jul 2016 10:41:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290573#M22506</guid>
      <dc:creator>CliveCartwright</dc:creator>
      <dc:date>2016-07-01T10:41:10Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Field Calculator Pre-Logic Script to pyscript</title>
      <link>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290574#M22507</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try this... if there are any errors it is because I can't test it from an iThingy since you can't run Arc* stuff on them&lt;/P&gt;&lt;P&gt;You basically want an update cursor&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/updatecursor.htm" title="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/updatecursor.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;UpdateCursor—Help | ArcGIS for Desktop&lt;/A&gt; &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
fc = "c:/path_to/Your_gdb/my.gdb/yourlayer"
field1 = "Shape_Area"
field2 = "MAX_RANK"
calc_fld = "Calculate_field"
cursor = arcpy.UpdateCursor(fc)
row = cursor.next()
while row:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # field2 will be equal to field1 multiplied by 3.0
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue(field1) == 0.0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(0.0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(calc_fld, row.getValue(field1))
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = cursor.next()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:00:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290574#M22507</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T14:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Field Calculator Pre-Logic Script to pyscript</title>
      <link>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290575#M22508</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The simplest way to get the proper syntax is to use your calculation in model builder then export as python script. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jul 2016 12:00:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290575#M22508</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2016-07-01T12:00:26Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Field Calculator Pre-Logic Script to pyscript</title>
      <link>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290576#M22509</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wes... which assumes the model works first go... but based on the number of model failures on this site, that would be my last choice &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jul 2016 12:03:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290576#M22509</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-07-01T12:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Field Calculator Pre-Logic Script to pyscript</title>
      <link>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290577#M22510</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Wes. I tried this and it returned:&lt;/P&gt;&lt;P&gt;Failed to execute. Parameters are not valid.&lt;/P&gt;&lt;P&gt;ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 1)&lt;/P&gt;&lt;P&gt;Failed to execute (CalculateField). &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/sad.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jul 2016 13:09:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290577#M22510</guid>
      <dc:creator>CliveCartwright</dc:creator>
      <dc:date>2016-07-01T13:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Field Calculator Pre-Logic Script to pyscript</title>
      <link>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290578#M22511</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Dan.&lt;/P&gt;&lt;P&gt;I've just tried this following your advice and sadly it returned a runtime error:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;fc = currentWorkspace + "/dataset"&lt;/P&gt;&lt;P&gt;field1 = "MAX_RANK"&lt;/P&gt;&lt;P&gt;field2 = "Shape_Area"&lt;/P&gt;&lt;P&gt;calc_fld = "Calculate_field"&lt;/P&gt;&lt;P&gt;cursor = arcpy.UpdateCursor(fc)&lt;/P&gt;&lt;P&gt;row = cursor.next()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;while row:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue(field1) == 0:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(0)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(calc_fld, row.getValue(field2))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = cursor.next()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Runtime error &lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 35, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 1040, in setValue&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return convertArcObjectToPythonObject(self._arc_object.SetValue(*gp_fixargs(args)))&lt;/P&gt;&lt;P&gt;AttributeError: Row: Error in parsing arguments for SetValue&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'll keep chipping away at it. &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jul 2016 13:12:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290578#M22511</guid>
      <dc:creator>CliveCartwright</dc:creator>
      <dc:date>2016-07-01T13:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Field Calculator Pre-Logic Script to pyscript</title>
      <link>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290579#M22512</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Could you post a screen shot of the model builder field calculator.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jul 2016 13:35:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290579#M22512</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2016-07-01T13:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Field Calculator Pre-Logic Script to pyscript</title>
      <link>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290580#M22513</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have made it work!!!!!!&lt;/P&gt;&lt;P&gt;Code I used:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-size: 9.0pt; font-family: 'Courier New'; color: #ed7d31;"&gt;# Populate SUBJECT_AREA only if there is a Rank shown in MAX_RANK field.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 9.0pt; font-family: 'Courier New'; color: #ed7d31;"&gt;expression = "LookUp(!MAX_RANK!, !Shape_Area!)"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 9.0pt; font-family: 'Courier New'; color: #ed7d31;"&gt;codeblock = "def LookUp(MAX_RANK, Shape_Area):\n\&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 9.0pt; font-family: 'Courier New'; color: #ed7d31;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if MAX_RANK == 0:\n\&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 9.0pt; font-family: 'Courier New'; color: #ed7d31;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0\n\&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 9.0pt; font-family: 'Courier New'; color: #ed7d31;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:\n\&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 9.0pt; font-family: 'Courier New'; color: #ed7d31;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return Shape_Area"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 9.0pt; font-family: 'Courier New'; color: #ed7d31;"&gt;gp.CalculateField_management(currentWorkspace + "/myDataset", "SUBJECT_AREA", expression, "PYTHON", codeblock)&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jul 2016 15:01:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/converting-field-calculator-pre-logic-script-to/m-p/290580#M22513</guid>
      <dc:creator>CliveCartwright</dc:creator>
      <dc:date>2016-07-01T15:01:10Z</dc:date>
    </item>
  </channel>
</rss>

