<?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: arcpy.CalculateField_management  not passing  (variable + !FieldValue!)  calculation in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39370#M3128</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;yes, that link has many good samples.&amp;nbsp; I usually don't have that many issues with the formatting, but what typically worked for me in the past didn't with CalculateField.&amp;nbsp; I'm glad I didn't need to deal with the code block, etc.&amp;nbsp; Anyway, now I have two ways to fix it....yours, which I will use....and my double calc if all else fails.&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 28 Oct 2015 19:41:38 GMT</pubDate>
    <dc:creator>RebeccaStrauch__GISP</dc:creator>
    <dc:date>2015-10-28T19:41:38Z</dc:date>
    <item>
      <title>arcpy.CalculateField_management  not passing  (variable + !FieldValue!)  calculation</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39364#M3122</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm trying to use arcpy.CalculateField in a script where I'm setting a "short" field to basically&amp;nbsp; "(int(TransSeriesNo) * 100) + !OID!"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have tried many incarnations of this based on the help docs and various geonet and other threads with no luck.&amp;nbsp; I have success if I hardcode the the first part of the equation (ie.&amp;nbsp; "int('!OID!') + 400") but of course that is not what I want to do.&amp;nbsp; Below are some of the various things I have tried....the error message, if there is one, is in the comment above the set.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;by the way, I know I can .format the better&amp;nbsp; ( &lt;A href="https://community.esri.com/migrated-users/3355" target="_blank"&gt;Curtis Price&lt;/A&gt;​ ) and will, but just trying to get it working first.&amp;nbsp; Thanks for any help&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#works
TransSeriesNo = "4"
setNo = (int(TransSeriesNo) * 100)
expression = "int('!OID!') + 400"
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3")

# doesn't work&amp;nbsp;&amp;nbsp; NameError: name 'setNo'is not defined
TransSeriesNo = "3"
setNo = (int(TransSeriesNo) * 100)
expression = "int('!OID!') + setNo"
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3")

# doesn't work&amp;nbsp;&amp;nbsp; NameError: name 'setNo'is not define
TransSeriesNo = "2"
setNo = (int(TransSeriesNo) * 100)
codeblock = "setNo"
expression = "int('!OID!') + setNo"
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3", codeblock)

#&amp;nbsp; doesn't work&amp;nbsp;&amp;nbsp; NameError: name 'setNo'is not define
TransSeriesNo = "1"
setNo = (int(TransSeriesNo) * 100)
expression = "getPtID(setNo, !OID!)"
codeblock = """def getPtID(setNo, theOID):
&amp;nbsp;&amp;nbsp;&amp;nbsp; setPtID = int(theOID + setNo)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return setPtID"""
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3", codeblock)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:31:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39364#M3122</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2021-12-10T21:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.CalculateField_management  not passing  (variable + !FieldValue!)  calculation</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39365#M3123</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm going to guess that setNo in expression needs to be formatted somehow. It may just be seen as a string as is.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Oct 2015 19:08:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39365#M3123</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2015-10-28T19:08:28Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.CalculateField_management  not passing  (variable + !FieldValue!)  calculation</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39366#M3124</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rebecca,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think in this case you will need to use some type of string formatting since you need to insert the value(an integer) into a string expression.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;See below&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG __jive_id="133849" alt="Formatting.png" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/133849_Formatting.png" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have this thread bookmarked as reference for string formatting examples, it gets confusing if you don't use it often.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/thread/121098"&gt;Python - Select by attributes query issue&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Oct 2015 19:14:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39366#M3124</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2015-10-28T19:14:51Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.CalculateField_management  not passing  (variable + !FieldValue!)  calculation</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39367#M3125</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Good guess, but I could never figure out the right way.&amp;nbsp; The issues seems to be that I'm mixing a variable or variable calculation with a field value.&amp;nbsp; Although this wasn't what I was going for, it dawned on me, if that was the problem I could just set the field to the variable, then calc it again adding the two fields.&amp;nbsp; That worked.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;TransSeriesNo = "9"
setNo = (int(TransSeriesNo) * 100)
arcpy.CalculateField_management("Pts1c", "testID", setNo , "PYTHON_9.3", codeblock)
arcpy.CalculateField_management("Pts1c", "testID", "!testID! + !OID!", "PYTHON_9.3", codeblock)
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With an added comment or two in my code, it may not be the most efficient way, but at least I finally got it to calc the values I expected.&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:31:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39367#M3125</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2021-12-10T21:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.CalculateField_management  not passing  (variable + !FieldValue!)  calculation</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39368#M3126</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ian, thanks.&amp;nbsp; I had something similar on several occasions, but had the " in the wrong place in the expresison.&amp;nbsp; Below is the basically the code I'll use:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;TransSeriesNo = "3"
setNo = (int(TransSeriesNo) * 100)
print setNo
expression = "int('!OID!') + {}".format(setNo)
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3")
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the quick help.&amp;nbsp; Been driving my batty for a day.&amp;nbsp; ugh!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:31:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39368#M3126</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2021-12-10T21:31:53Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.CalculateField_management  not passing  (variable + !FieldValue!)  calculation</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39369#M3127</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Quite welcome, sometimes making valid expressions like that with python can be a pain, hence why I found myself a reference to go back to.&amp;nbsp; I don't use python quite enough to be fully comfortable with string formatting without a guide.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Oct 2015 19:30:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39369#M3127</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2015-10-28T19:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.CalculateField_management  not passing  (variable + !FieldValue!)  calculation</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39370#M3128</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;yes, that link has many good samples.&amp;nbsp; I usually don't have that many issues with the formatting, but what typically worked for me in the past didn't with CalculateField.&amp;nbsp; I'm glad I didn't need to deal with the code block, etc.&amp;nbsp; Anyway, now I have two ways to fix it....yours, which I will use....and my double calc if all else fails.&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Oct 2015 19:41:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39370#M3128</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2015-10-28T19:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.CalculateField_management  not passing  (variable + !FieldValue!)  calculation</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39371#M3129</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;I'm trying to use arcpy.CalculateField in a script where I'm &lt;/P&gt;&lt;P&gt;setting a "short" field to basically&amp;nbsp; "(int(TransSeriesNo) * 100) + !OID!"&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#&amp;nbsp; no .format
expr = "(int(" + str(TransSeriesNo) + ") * 100) + !OID!"
# format
expr = "(int({}) * 100) + !OID!".format(TransSeriesNo)
# or, if TransSeriesNo is an integer (1) or string with no decimals ("1"):
expr = "(" + str(TransSeriesNo)&amp;nbsp; + " * 100) + !OID!)"
# .format:
expr = "({} * 100) + !OID!".format(transSeriesNo)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hints:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. The calculate value expression must be entirely a string&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. Field definitions (eg !OID!) are replaced at runtime with the field value for each row at runtime &lt;EM&gt;'in data type'&lt;/EM&gt; (!OID! will be integer and doesn't need an int() around it, and a string field would not need quotes around it for its to be recognized as a string). For example:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;expr = "!CITY! + ' ' + !STATE! + ' ' + !ZIP!"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:31:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-not-passing/m-p/39371#M3129</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-10T21:31:56Z</dc:date>
    </item>
  </channel>
</rss>

