<?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: Correct Codeblock Python syntax for CalculateField in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246500#M66467</link>
    <description>&lt;P&gt;I saw that and thought I just deleted one. It popped an error message when I first clicked the submit button.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 09 Jan 2023 16:22:22 GMT</pubDate>
    <dc:creator>MPach</dc:creator>
    <dc:date>2023-01-09T16:22:22Z</dc:date>
    <item>
      <title>Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246494#M66465</link>
      <description>&lt;P&gt;&amp;nbsp;I'm using ArcMap 10.8.1 so this is Python 2.7.&lt;/P&gt;&lt;P&gt;&amp;nbsp; I'm trying to find the correct syntax to troubleshoot/debug this script that I'm working on. I was using the Python interpreter going line by line trying to resolve the issue and can't seem to figure out how to enter the codeblock into the interpreter. When I try to enter the codeblock as is I get a Syntax Error and I'm not skilled enough to know how to resolve it yet. It seems like the interpreter doesn't like the triple quotes for the codeblock. Any help is greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;expression = 'reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)

codeblock = """ def reclass(lengthField,widthField,num):
                   if ((lengthField/num) &amp;gt;= (widthField)):
                       return 'True'
                   else:
                       return '' """&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 16:12:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246494#M66465</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-09T16:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246499#M66466</link>
      <description>&lt;P&gt;Double post. See&amp;nbsp;&lt;A href="https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246493#M66464" target="_blank"&gt;Correct Codeblock Python syntax for CalculateField - Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 16:20:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246499#M66466</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-01-09T16:20:13Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246500#M66467</link>
      <description>&lt;P&gt;I saw that and thought I just deleted one. It popped an error message when I first clicked the submit button.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 16:22:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246500#M66467</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-09T16:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246513#M66468</link>
      <description>&lt;P&gt;I don't think you can have leading and trailing spaces in the string since the code block needs valid python and the space is offsetting the indents.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;expression = 'reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)

codeblock = """def reclass(lengthField,widthField,num):
                   return 'True' if (lengthField/num) &amp;gt;= widthField else ''"""&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 09 Jan 2023 16:48:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246513#M66468</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-01-09T16:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246520#M66469</link>
      <description>&lt;P&gt;The syntax you are debugging is specific to using field calculator in Pro or ArcMap when updating a table's field (using the Python Expression Type).&amp;nbsp; You could try entering it there and testing it (I.E. the expression in the top box and the code block below like this)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KimGarbade_0-1673282672307.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/59984iC43C1628C44DF090/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KimGarbade_0-1673282672307.png" alt="KimGarbade_0-1673282672307.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I debugged it using Python in Spyder like this (since python doesn't understand the syntax "CodeBlock" as far as I know, and the code block itself is just a python function.):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-
"""
Created on Mon Jan  9 11:17:59 2023

@author: Tester
"""

lengthField = 10
widthField = 5
num = 2

expression = "reclass(!%d!,!%d!,!%d!)" % (lengthField, widthField, num)


def reclass(lengthField,widthField,num):
                   if ((lengthField/num) &amp;gt;= (widthField)):
                       return 'True'
                   else:
                       return ''
                   
print (expression)
print (reclass(lengthField,widthField,num))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; The result was&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KimGarbade_2-1673283097760.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/59988iE4CC3B20592CE29A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KimGarbade_2-1673283097760.png" alt="KimGarbade_2-1673283097760.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I hope I understood you question correctly and that this is helpful.&lt;/P&gt;&lt;P&gt;K&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 16:55:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246520#M66469</guid>
      <dc:creator>KimberlyGarbade</dc:creator>
      <dc:date>2023-01-09T16:55:32Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246523#M66470</link>
      <description>&lt;P&gt;Thank Jeff, let me give that a shot. Hadn't even crossed my mind.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 16:57:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246523#M66470</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-09T16:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246525#M66471</link>
      <description>&lt;P&gt;You must be using a feature I don't know about. I can see you are using ArcMap and Calculate Field but I am not sure where the strings are coming from. Send screenshots? Especially the error message.&lt;/P&gt;&lt;P&gt;When I save a field calc to a file it creates a *.cal file that looks like this&lt;/P&gt;&lt;LI-CODE lang="python"&gt;reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)
__esri_field_calculator_splitter__
def reclass(lengthField,widthField,num):
                   if ((lengthField/num) &amp;gt;= (widthField)):
                       return 'True'
                   else:
                       return '' """&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In any case if I wanted to look at the syntax I would pull the code out of there and put it into a debugger, I go on and on about the wonders of Visual Studio Code but in your case you already have "Idle" installed so use that.&lt;/P&gt;&lt;P&gt;Then you can paste your code in there and play with it.&lt;/P&gt;&lt;P&gt;The first comment I have is that you PROBABLY meant something like this.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;# This returns True or False
def reclass(lengthField, widthField, num):
	return lengthField/num &amp;gt;= widthField

# Your code returns strings, which could work but will end up being confusing.
return 'True'
# is not the same as
return True&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a screenshot of my little test in Idle. After 1 minute of using Idle I want to use something else. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I already mentioned Visual Studio Code but a lot of people here like to use "Spyder" which is also good.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2023-01-09 085420.png" style="width: 841px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/59989i91318A2C5C1636D7/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2023-01-09 085420.png" alt="Screenshot 2023-01-09 085420.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I just did a quick check of docs for ArcGIS Desktop and on the Esri learning site and it looks like they have taken down anything useful for ArcMap. Maybe I am totally wrong and if so someone please correct me and put a link here for users who need learning resources in ArcMap??? The links I saw in the ArcMap docs just pointed at the generic Python.ORG site which is a really difficult learning path.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you can find a copy of this book: "Python Scripting for ArcGIS" by Paul A. Zandbergen, it would be useful but geez, Amazon wants $60 for it, at this point in time it's worth $5. I probably threw away my copy already.&lt;/P&gt;&lt;P&gt;With ArcGIS Pro using Python is easier in many ways, but I don't know your circumstances so that's up to you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 17:05:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246525#M66471</guid>
      <dc:creator>Brian_Wilson</dc:creator>
      <dc:date>2023-01-09T17:05:45Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246591#M66474</link>
      <description>&lt;P&gt;That was the problem. The space I had after the """.&amp;nbsp; Those things will drive you nuts if you don't see them. Thank you&amp;nbsp;@Anonymous User&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 20:07:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246591#M66474</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-09T20:07:19Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246594#M66475</link>
      <description>&lt;P&gt;I was using the python interpreter tool for ArcMap. I attached a screen shot for you below. I was creating a script for some automation and was getting a generic syntax error when from the geoprocessing results window, so I was going line by line trying to troubleshoot it rather than running the tool over and over again with a few code changes each time I wanted to be able to change the one line I knew I was having a problem with for a quicker easier fix. Nothing fancy, I just knew I had a syntax problem with a specific line of code and couldn't figure out what the problem was.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the very thorough response though, it was much appreciate. I have the book you're referring to and another by John Zelle that is also excellent too "Python Programming: An Introductory into Computer Science" 2nd Ed. I used to be pretty proficient with Python, but after not using it for a bunch of years b/c of a job change I'm kind of starting over again. I'm slightly over most of baby steps, but still making the baby step mistakes&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":frowning_face:"&gt;☹️&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MPach_0-1673294972429.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/60011i77E6E715D620FD9E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MPach_0-1673294972429.png" alt="MPach_0-1673294972429.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 20:17:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246594#M66475</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-09T20:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246595#M66476</link>
      <description>&lt;P&gt;Also, I didn't want to return a 'False' string, I was just returning an empty string '', so there the field was left blank when it wasn't True. It just makes things visually easier when looking down the the column and I think it just looks cleaner that way.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 20:20:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246595#M66476</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-09T20:20:58Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246597#M66477</link>
      <description>&lt;P&gt;Thanks Kim, I hope my other explanations helped out. I appreciate the help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 20:22:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246597#M66477</guid>
      <dc:creator>MPach</dc:creator>
      <dc:date>2023-01-09T20:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Codeblock Python syntax for CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246639#M66482</link>
      <description>&lt;P&gt;AHhhh, now that makes sense. Either "True" or "" goes into the attribute as its new value. Okay.&lt;/P&gt;&lt;P&gt;Passing Python code uninterpreted into a function like that creeps me out. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; But that's me. I bet you can pass the strings to "exec" to test. After all, Esri must be doing something like that.&lt;/P&gt;&lt;P&gt;Yes so you can do this, but it runs fine, no errors.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2023-01-09 150110.png" style="width: 713px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/60015i216A813026BA49BE/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2023-01-09 150110.png" alt="Screenshot 2023-01-09 150110.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jan 2023 23:02:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/correct-codeblock-python-syntax-for-calculatefield/m-p/1246639#M66482</guid>
      <dc:creator>Brian_Wilson</dc:creator>
      <dc:date>2023-01-09T23:02:04Z</dc:date>
    </item>
  </channel>
</rss>

