<?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: Syntax when passing variable to calculate field expression in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556673#M43520</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I got it to work!!! Nice way to close out the day...for some reason the OID field needed single quotes too. If you want to modify your code I'll mark your answer as accepted.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cluster_Plot = '"{}-{}"'.format(i,'!OBJECTID!')&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 14 Sep 2016 20:03:30 GMT</pubDate>
    <dc:creator>ZacharyHart</dc:creator>
    <dc:date>2016-09-14T20:03:30Z</dc:date>
    <item>
      <title>Syntax when passing variable to calculate field expression</title>
      <link>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556669#M43516</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK, so I feel like I've tried just about everything here. What I'm looking to do is to concatenate a iteration value (?) from a search cursor with the object ID of a feature class to create a unique ID field. I want these two values separated by a hyphen '-'. Target field (PLOT_ID) is obviously a string. There is what I've got working so far.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i = 1&lt;/P&gt;&lt;P&gt;ptCur = arcpy.da.SearchCursor (ptFC, fldLst)&lt;/P&gt;&lt;P&gt;for row in ptCur:&lt;/P&gt;&lt;P&gt;[various things happen removed for brevity]&lt;/P&gt;&lt;P&gt;Cluster_Plot = str(i) + '!OBJECTID!'&lt;/P&gt;&lt;P&gt;arcpy.AddMessage(Cluster_Plot)&lt;/P&gt;&lt;P&gt;arcpy.CalculateField_management(outPointsDissolve,"PLOT_ID",Cluster_Plot,"PYTHON_9.3")&lt;/P&gt;&lt;P&gt;things are appended to a a target FC and the target field looks like this:&lt;BR /&gt;&lt;IMG alt="" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/227941_pastedImage_8.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So it appears to be treating the expression inputs as strings. From here I've tried to get a - in between the two values with no luck....even when converting the OID to a string : 'str(!OBJECTID!)'&amp;nbsp;&amp;nbsp; I just can't get things to escape right i guess. Things I've tried:&lt;/P&gt;&lt;P&gt;Cluster_Plot = str(i) + '-' + '!OBJECTID!'&lt;/P&gt;&lt;P&gt;Here the&amp;nbsp; hyphen apparently is interpreted as an operator:&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="image-2 jive-image" src="https://community.esri.com/legacyfs/online/227943_pastedImage_12.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cluster_Plot = str(i) + " + '-' + " + '!OBJECTID!'&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="image-3 jive-image" src="https://community.esri.com/legacyfs/online/227944_pastedImage_16.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cluster_Plot = str(i) + " + '-' + " + 'str(!OBJECTID!)'&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="jive-image image-4" src="https://community.esri.com/legacyfs/online/227945_pastedImage_20.png" /&gt;&lt;/P&gt;&lt;P&gt;I dont get this because you can use the + operator in the field calculator just fine (if you format the field as a string using str)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Could someone please help me out of my misery here?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 14 Sep 2016 18:36:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556669#M43516</guid>
      <dc:creator>ZacharyHart</dc:creator>
      <dc:date>2016-09-14T18:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax when passing variable to calculate field expression</title>
      <link>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556670#M43517</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Zachary,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Would&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;Cluster_Plot &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'"{}-{}"'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;i&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'!OBJECTID!'&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;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;work? I've become a big fan of using triple quoted strings and the .format() method when creating a variable to use as a field calculator expression.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;Another option is to use an update cursor. Something like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;table &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"myTable"&lt;/SPAN&gt;
fields &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"OBJECTID"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"PLOT_ID"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
i &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;UpdateCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;table&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; fields&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; cursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;i&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"-"&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;updateRow&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i&lt;SPAN class="operator token"&gt;+=&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&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;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:02:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556670#M43517</guid>
      <dc:creator>MicahBabinski</dc:creator>
      <dc:date>2021-12-12T00:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax when passing variable to calculate field expression</title>
      <link>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556671#M43518</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Micah...I'm just getting a generic 'invalid syntax' error. for the first option.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The second wont work because the OIDs aren't coming from the FC used to run the loop/search cursor.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 14 Sep 2016 18:53:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556671#M43518</guid>
      <dc:creator>ZacharyHart</dc:creator>
      <dc:date>2016-09-14T18:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax when passing variable to calculate field expression</title>
      <link>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556672#M43519</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I see. Can you post your full script? Also, maybe try the field calculator expression without the str() around the i variable and !OBJECTID!.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 14 Sep 2016 18:57:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556672#M43519</guid>
      <dc:creator>MicahBabinski</dc:creator>
      <dc:date>2016-09-14T18:57:26Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax when passing variable to calculate field expression</title>
      <link>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556673#M43520</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I got it to work!!! Nice way to close out the day...for some reason the OID field needed single quotes too. If you want to modify your code I'll mark your answer as accepted.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cluster_Plot = '"{}-{}"'.format(i,'!OBJECTID!')&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 14 Sep 2016 20:03:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556673#M43520</guid>
      <dc:creator>ZacharyHart</dc:creator>
      <dc:date>2016-09-14T20:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Syntax when passing variable to calculate field expression</title>
      <link>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556674#M43521</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Terrific! I've modified it. If at first you don't succeed...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 14 Sep 2016 20:07:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/syntax-when-passing-variable-to-calculate-field/m-p/556674#M43521</guid>
      <dc:creator>MicahBabinski</dc:creator>
      <dc:date>2016-09-14T20:07:12Z</dc:date>
    </item>
  </channel>
</rss>

