<?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: including Python variable in codeblock in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308420#M24005</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I converted to question (Actions menu, upper right) and gave Darren his deserved cookie.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Keith, I know you're new to Python but have to mention this: the CalculateField tool is great&amp;nbsp;in Desktop and ModelBuilder, but in many cases update cursors are FAR easier for this kind of thing in scripting:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To wit:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;val &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;
inc &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;&lt;SPAN class="string token"&gt;"xx4_ncard"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"as_drv_srt"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; rows&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; rows&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;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; val
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val &lt;SPAN class="operator token"&gt;+=&lt;/SPAN&gt; inc
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;updateRow&lt;SPAN class="punctuation token"&gt;(&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;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 14:45:44 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2021-12-11T14:45:44Z</dc:date>
    <item>
      <title>including Python variable in codeblock</title>
      <link>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308416#M24001</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can anyone explain what I've done wrong here. Have a feeling this is really simple...(new to Python). This code si being pasted into the Immediate window in ArcMap 10.1, in order to calculate a field with an "incrementing" value. I would like to specify the starting value externally.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This works:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"xx4_ncard"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"as_drv_srt"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"autoIncrement()"&lt;/SPAN&gt;&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;"rec&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;\ndef autoIncrement&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;\n &lt;SPAN class="keyword token"&gt;global&lt;/SPAN&gt; rec\n pStart &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;1&lt;/SPAN&gt; &lt;SPAN class="comment token"&gt;#adjust start value, if req'd \n pInterval = 1 #adjust interval value, if req'd\n if (rec == 0): \n&amp;nbsp; rec = pStart \n else: \n&amp;nbsp; rec = rec + pInterval \n return rec")&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;This doesn't:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;start &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;

arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"xx4_ncard"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"as_drv_srt"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"autoIncrement()"&lt;/SPAN&gt;&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="string token"&gt;"rec=0\ndef autoIncrement():\n global rec\n pStart = "&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; start &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; " &lt;SPAN class="comment token"&gt;#adjust start value, if req'd \n pInterval = 1 #adjust interval value, if req'd\n if (rec == 0): \n&amp;nbsp; rec = pStart \n else: \n&amp;nbsp; rec = rec + pInterval \n return rec")&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>Sat, 11 Dec 2021 14:45:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308416#M24001</guid>
      <dc:creator>KeithD1</dc:creator>
      <dc:date>2021-12-11T14:45:42Z</dc:date>
    </item>
    <item>
      <title>Re: including Python variable in codeblock</title>
      <link>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308417#M24002</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can't concatenate a string and a number. Convert the number to a string before concatenating: str(start)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Sep 2016 23:28:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308417#M24002</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2016-09-22T23:28:58Z</dc:date>
    </item>
    <item>
      <title>Re: including Python variable in codeblock</title>
      <link>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308418#M24003</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for the quick reply - the code now works as expected.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Sep 2016 23:35:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308418#M24003</guid>
      <dc:creator>KeithD1</dc:creator>
      <dc:date>2016-09-22T23:35:45Z</dc:date>
    </item>
    <item>
      <title>Re: including Python variable in codeblock</title>
      <link>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308419#M24004</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;For future reference, please mark questions as questions, instead of discussions.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Sep 2016 19:11:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308419#M24004</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2016-09-23T19:11:16Z</dc:date>
    </item>
    <item>
      <title>Re: including Python variable in codeblock</title>
      <link>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308420#M24005</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I converted to question (Actions menu, upper right) and gave Darren his deserved cookie.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Keith, I know you're new to Python but have to mention this: the CalculateField tool is great&amp;nbsp;in Desktop and ModelBuilder, but in many cases update cursors are FAR easier for this kind of thing in scripting:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To wit:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;val &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;
inc &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;&lt;SPAN class="string token"&gt;"xx4_ncard"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"as_drv_srt"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; rows&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; rows&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;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; val
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val &lt;SPAN class="operator token"&gt;+=&lt;/SPAN&gt; inc
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;updateRow&lt;SPAN class="punctuation token"&gt;(&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;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:45:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/including-python-variable-in-codeblock/m-p/308420#M24005</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T14:45:44Z</dc:date>
    </item>
  </channel>
</rss>

