<?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: Field calculation in Python - expression not defined in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/field-calculation-in-python-expression-not-defined/m-p/723574#M23973</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You have to quote the expression in single quotes in the style of an SQL query because that is what it is.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management(Input, "Filename","'"+ expression+"'", "PYTHON")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To debug the expressions run the tool interactively, then grab a Python snippet from the results panel and paste it into your script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This will show you some working code that you can then edit to replace parts with a variable.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In a script it is actually easier to use a cursor than the CalculateField which is really only designed for Modelbuilder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A cursor is just as fast, can be much easier to write and debug and can handle errors such as null values with an if statement.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(Untested pseudocode)&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;with arcpy.da.UpdateCursor(Input,['filename']) as cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = expression
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cur.updateRow(row)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;No gymnastics needed to quote strings. The with closes the cursor.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 06:57:05 GMT</pubDate>
    <dc:creator>KimOllivier</dc:creator>
    <dc:date>2021-12-12T06:57:05Z</dc:date>
    <item>
      <title>Field calculation in Python - expression not defined</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculation-in-python-expression-not-defined/m-p/723573#M23972</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;BR /&gt;&lt;SPAN&gt;Apologies if something similar has been asked before in a previous post. I'm trying to add a field and then calculate it with the name of the file. The python script I'm trying to run below seems to run okay however it falls over at the calculate field step of the process.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-style:italic;"&gt;import os, arcpy&lt;BR /&gt;&lt;BR /&gt;Input = "C:\\GIS\\TempFile.shp"&lt;BR /&gt;expression = os.path.basename(Input).rstrip(os.path.splitext(Input)[1])&lt;BR /&gt;print Input&lt;BR /&gt;print expression&lt;BR /&gt;&lt;BR /&gt;# Add field called "Filename"&lt;BR /&gt;arcpy.AddField_management("TempFile","Filename","TEXT",254)&lt;BR /&gt;&lt;BR /&gt;# Calculate field with file name&lt;BR /&gt;arcpy.CalculateField_management(Input, "Filename", expression, "PYTHON")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The error that comes up is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000539: Error running expression: expression &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;NameError: name 'expression' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However I find this confusing as I would've thought the line below defines "expression".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;expression = os.path.basename(Input).rstrip(os.path.splitext(Input)[1])&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I feel I'm pretty close to getting it however I need a helping hand from somewhere. Any feedback would be welcomed.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Oct 2013 03:00:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculation-in-python-expression-not-defined/m-p/723573#M23972</guid>
      <dc:creator>MichaelGresham</dc:creator>
      <dc:date>2013-10-02T03:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculation in Python - expression not defined</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculation-in-python-expression-not-defined/m-p/723574#M23973</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You have to quote the expression in single quotes in the style of an SQL query because that is what it is.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management(Input, "Filename","'"+ expression+"'", "PYTHON")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To debug the expressions run the tool interactively, then grab a Python snippet from the results panel and paste it into your script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This will show you some working code that you can then edit to replace parts with a variable.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In a script it is actually easier to use a cursor than the CalculateField which is really only designed for Modelbuilder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A cursor is just as fast, can be much easier to write and debug and can handle errors such as null values with an if statement.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(Untested pseudocode)&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;with arcpy.da.UpdateCursor(Input,['filename']) as cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = expression
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cur.updateRow(row)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;No gymnastics needed to quote strings. The with closes the cursor.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:57:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculation-in-python-expression-not-defined/m-p/723574#M23973</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2021-12-12T06:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculation in Python - expression not defined</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculation-in-python-expression-not-defined/m-p/723575#M23974</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Kim,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I hope all is well with yourself back in NZ. Thanks for that - both sets of code worked for this problem. I will have to explore the arcpy.da cursor functions a bit more to find out what they offer instead of the standard python snippets from modelbuilder :).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Oct 2013 03:15:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculation-in-python-expression-not-defined/m-p/723575#M23974</guid>
      <dc:creator>MichaelGresham</dc:creator>
      <dc:date>2013-10-17T03:15:06Z</dc:date>
    </item>
  </channel>
</rss>

