<?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 Using Python for Calculate Field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-python-for-calculate-field/m-p/404576#M31868</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am new to Python and was hoping someone could help me with this code. I just want to do a simple calculate field (as part of a larger script) and calculate one field to a few characters of text. All the examples I found online were much more complicated than what I needed help with. The following code results this error message:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Expected end of statement&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (CalculateField).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I get the error message for the first Calculate Field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]&lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
from arcpy import env

# Define variables

env.workspace = r"Z:\package\CPP_scratch.gdb"

# This file name will change each month
eoptreps_v2 = r"Z:\package\Polygon_package.gdb\eoptreps_20120806"

#####

print "Calculating project field - 1307"
arcpy.CalculateField_management(eoptreps_v2, "Project", "1307 - PNDI CPP")

print "Calculating project field - 1350"

if arcpy.Exists("eoptreps_lyr"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("eoptreps_lyr")
arcpy.MakeFeatureLayer_management(eoptreps_v2, "eoptreps_lyr")

if arcpy.Exists("StateForest_lyr"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("StateForest_lyr")
arcpy.MakeFeatureLayer_management(r"Z:\StateLayers.gdb\Boundaries_Natural\PA_StateForest", "StateForest_lyr")

arcpy.SelectLayerByLocation_management("eoptreps_lyr", "INTERSECT", "StateForest_lyr")

arcpy.CalculateField_management("eoptreps_lyr", "Project", "1350 - DCNR")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;SPAN&gt;[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Things I have tried:&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;Changing "1307 - PNDI CPP" (the expression) to '[Project] = "1307 - PNDI CPP"' This results in the script running as if it works, but then when I check the field it is still NULL.&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Changing the expression to "PGLMT PNDI" in case the dash was causing a problem. Same error message here.&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 07 Aug 2012 19:03:34 GMT</pubDate>
    <dc:creator>KristenE</dc:creator>
    <dc:date>2012-08-07T19:03:34Z</dc:date>
    <item>
      <title>Using Python for Calculate Field</title>
      <link>https://community.esri.com/t5/python-questions/using-python-for-calculate-field/m-p/404576#M31868</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am new to Python and was hoping someone could help me with this code. I just want to do a simple calculate field (as part of a larger script) and calculate one field to a few characters of text. All the examples I found online were much more complicated than what I needed help with. The following code results this error message:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Expected end of statement&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (CalculateField).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I get the error message for the first Calculate Field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]&lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
from arcpy import env

# Define variables

env.workspace = r"Z:\package\CPP_scratch.gdb"

# This file name will change each month
eoptreps_v2 = r"Z:\package\Polygon_package.gdb\eoptreps_20120806"

#####

print "Calculating project field - 1307"
arcpy.CalculateField_management(eoptreps_v2, "Project", "1307 - PNDI CPP")

print "Calculating project field - 1350"

if arcpy.Exists("eoptreps_lyr"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("eoptreps_lyr")
arcpy.MakeFeatureLayer_management(eoptreps_v2, "eoptreps_lyr")

if arcpy.Exists("StateForest_lyr"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("StateForest_lyr")
arcpy.MakeFeatureLayer_management(r"Z:\StateLayers.gdb\Boundaries_Natural\PA_StateForest", "StateForest_lyr")

arcpy.SelectLayerByLocation_management("eoptreps_lyr", "INTERSECT", "StateForest_lyr")

arcpy.CalculateField_management("eoptreps_lyr", "Project", "1350 - DCNR")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;SPAN&gt;[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Things I have tried:&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;Changing "1307 - PNDI CPP" (the expression) to '[Project] = "1307 - PNDI CPP"' This results in the script running as if it works, but then when I check the field it is still NULL.&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Changing the expression to "PGLMT PNDI" in case the dash was causing a problem. Same error message here.&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Aug 2012 19:03:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-for-calculate-field/m-p/404576#M31868</guid>
      <dc:creator>KristenE</dc:creator>
      <dc:date>2012-08-07T19:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using Python for Calculate Field</title>
      <link>https://community.esri.com/t5/python-questions/using-python-for-calculate-field/m-p/404577#M31869</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Please read this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code"&gt;http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As to your problem. What are your field names and types?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Aug 2012 19:10:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-for-calculate-field/m-p/404577#M31869</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-08-07T19:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: Using Python for Calculate Field</title>
      <link>https://community.esri.com/t5/python-questions/using-python-for-calculate-field/m-p/404578#M31870</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You need string delimeters inside the expression, which is itself a string. In Python this is easily done by escaping the double quote, or using the other kind of python string delimeter outside:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
arcpy.CalculateField_management(eoptreps_v2, "Project", "\"1307 - PNDI CPP\"")
arcpy.CalculateField_management(eoptreps_v2, "Project", '"1307 - PNDI CPP"')
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Double-quotes are used here because although we are using python, the default syntax expression for the Calculate Field tool is VBScript, which delimits string literals with double quotes.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:25:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-for-calculate-field/m-p/404578#M31870</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T18:25:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using Python for Calculate Field</title>
      <link>https://community.esri.com/t5/python-questions/using-python-for-calculate-field/m-p/404579#M31871</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you are in a Python script already I recommend that you do not use CalculateField, use a cursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;CalculateField is useful in ModelBuilder and interactive table manipulation but not in a script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It is no faster than a cursor (especially at 10.1 da module)&amp;nbsp; because CalculateField simply wraps a cursor around the expression.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;With a cursor you can:&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;Trap errors with a try/except block&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Test for unexpected data and handle it properly (eg null values)&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Debug easily by adding print statements&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Write the expressions without complicated syntax (no !wrappers! or [access_speclal] brackets)&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Do multiple field calculations at once&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Use a dictionary to update values to avoid a join&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Aug 2012 23:49:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-python-for-calculate-field/m-p/404579#M31871</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2012-08-08T23:49:27Z</dc:date>
    </item>
  </channel>
</rss>

