<?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: Error in calculate field using Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565792#M44317</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you. It worked.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 15 Dec 2017 14:38:28 GMT</pubDate>
    <dc:creator>DuminduJayasekera</dc:creator>
    <dc:date>2017-12-15T14:38:28Z</dc:date>
    <item>
      <title>Error in calculate field using Python</title>
      <link>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565789#M44314</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have the following python script&amp;nbsp;to create a TEXT column called "SEC" and then I need to perform a Field calculator operation for the SEC column. I want to read the corresponding string in the "mtrs" column and and fill in SEC column. I have performed this task manually and it worked. I need to slice the "mtrs" string like this&amp;nbsp;"S{}".format(int( !mtrs! [17:19]))" and fill in SEC column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My script below give me an error in line 14. SyntaxError. invalid syntax.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can somebody help me to fix this and make this work?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;# Import system modules

import arcpy
from arcpy import env
 
# Set environment settings
env.workspace = "H:/Test/Points.gdb"
 
# Set local variables
inFeatures = "plsssection_test_p1"
fieldName1 = "SEC"
length = 20

expression = ""S{}".format(int( !mtrs! [17:19]))"
 
# Execute AddField twice for two new fields
arcpy.AddField_management(inFeatures, fieldName1, "TEXT", length, "", "")
arcpy.CalculateField_management(inFeatures, "SEC",expression, "PYTHON_9.3")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:23:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565789#M44314</guid>
      <dc:creator>DuminduJayasekera</dc:creator>
      <dc:date>2021-12-12T00:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Error in calculate field using Python</title>
      <link>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565790#M44315</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What about:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="comment token"&gt;# Import system modules&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; arcpy &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; env
 
&lt;SPAN class="comment token"&gt;# Set environment settings&lt;/SPAN&gt;
env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"H:/Test/Points.gdb"&lt;/SPAN&gt;
 
&lt;SPAN class="comment token"&gt;# Set local variables&lt;/SPAN&gt;
inFeatures &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"plsssection_test_p1"&lt;/SPAN&gt;
fieldName1 &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"SEC"&lt;/SPAN&gt;
length &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;20&lt;/SPAN&gt;

expression &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"!mtrs![17:19]"&lt;/SPAN&gt;
 
&lt;SPAN class="comment token"&gt;# Execute AddField twice for two new fields&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;inFeatures&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; fieldName1&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"TEXT"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; length&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;inFeatures&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"SEC"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;expression&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"PYTHON_9.3"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:23:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565790#M44315</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-12T00:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: Error in calculate field using Python</title>
      <link>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565791#M44316</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;a &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;S&lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;".format(int( !mtrs! [17:19]))"&lt;/SPAN&gt;
&amp;nbsp; File &lt;SPAN class="string token"&gt;"&amp;lt;ipython-input-1-6325b07dafa7&amp;gt;"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; line &lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; a &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;S&lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;".format(int( !mtrs! [17:19]))"&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="operator token"&gt;^&lt;/SPAN&gt;
SyntaxError&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; invalid syntax&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;watch your quotes... throw a single around the whole expression&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;a &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'"S{}".format(int( !mtrs! [17:19]))'&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# --- no error&lt;/SPAN&gt;
&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:23:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565791#M44316</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-12T00:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: Error in calculate field using Python</title>
      <link>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565792#M44317</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you. It worked.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Dec 2017 14:38:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565792#M44317</guid>
      <dc:creator>DuminduJayasekera</dc:creator>
      <dc:date>2017-12-15T14:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: Error in calculate field using Python</title>
      <link>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565793#M44318</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the reply.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Dec 2017 14:39:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-calculate-field-using-python/m-p/565793#M44318</guid>
      <dc:creator>DuminduJayasekera</dc:creator>
      <dc:date>2017-12-15T14:39:04Z</dc:date>
    </item>
  </channel>
</rss>

