<?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 Python, ArcGIS and Weird String Error in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-arcgis-and-weird-string-error/m-p/429164#M33733</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a FC that I am working with in Python.&amp;nbsp; I add a text field to it and want to calculate the field values based on a string variable.&amp;nbsp; The string is read in and parsed to get the value.&amp;nbsp; Example: Python reads in "1603-01_mp2.0", splits the string at the underscore, and uses the first item in the split: 1603-01.&amp;nbsp; So, line = 1603-01.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then, to make it even MORE of a string, I do lineName = "Line"+line.&amp;nbsp; This results in "Line1603-01".&amp;nbsp; I know this to be good, because I have used print lineName to check.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Fast forward to where I try to calculate the value of the text field in the FC.&amp;nbsp; I use this python line:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateField_management(TempPLCL_Point, "Route", lineName, "PYTHON", "")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;During this calculation, though, it appears that either ArcGIS or Python wants to treat the - as a minus sign and do math on the string.&amp;nbsp; I get the following error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 000539: Error running expression: Line1603-01 &amp;lt;type 'exceptions.NameError'&amp;gt;: name 'Line1603' is not defined&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;How can I get past this seemingly simple problem that shouldn't be a problem at all?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 29 Mar 2013 19:40:17 GMT</pubDate>
    <dc:creator>BradOleson</dc:creator>
    <dc:date>2013-03-29T19:40:17Z</dc:date>
    <item>
      <title>Python, ArcGIS and Weird String Error</title>
      <link>https://community.esri.com/t5/python-questions/python-arcgis-and-weird-string-error/m-p/429164#M33733</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a FC that I am working with in Python.&amp;nbsp; I add a text field to it and want to calculate the field values based on a string variable.&amp;nbsp; The string is read in and parsed to get the value.&amp;nbsp; Example: Python reads in "1603-01_mp2.0", splits the string at the underscore, and uses the first item in the split: 1603-01.&amp;nbsp; So, line = 1603-01.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then, to make it even MORE of a string, I do lineName = "Line"+line.&amp;nbsp; This results in "Line1603-01".&amp;nbsp; I know this to be good, because I have used print lineName to check.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Fast forward to where I try to calculate the value of the text field in the FC.&amp;nbsp; I use this python line:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateField_management(TempPLCL_Point, "Route", lineName, "PYTHON", "")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;During this calculation, though, it appears that either ArcGIS or Python wants to treat the - as a minus sign and do math on the string.&amp;nbsp; I get the following error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 000539: Error running expression: Line1603-01 &amp;lt;type 'exceptions.NameError'&amp;gt;: name 'Line1603' is not defined&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;How can I get past this seemingly simple problem that shouldn't be a problem at all?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Mar 2013 19:40:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-arcgis-and-weird-string-error/m-p/429164#M33733</guid>
      <dc:creator>BradOleson</dc:creator>
      <dc:date>2013-03-29T19:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: Python, ArcGIS and Weird String Error</title>
      <link>https://community.esri.com/t5/python-questions/python-arcgis-and-weird-string-error/m-p/429165#M33734</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It would be nice to see the whole code snippet but I think I see what your problem is.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Your code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateField_management(TempPLCL_Point, "Route", &lt;/SPAN&gt;&lt;STRONG&gt;lineName&lt;/STRONG&gt;&lt;SPAN&gt;, "PYTHON", "")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ArcGIS Help 10.1:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateField_management (in_table, field, &lt;/SPAN&gt;&lt;STRONG&gt;expression&lt;/STRONG&gt;&lt;SPAN&gt;, {expression_type}, {code_block})&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I do lineName = "Line"+line&lt;/BLOCKQUOTE&gt;&lt;SPAN&gt;I have made bold the problem.&amp;nbsp; It seems that you are trying to pass in a string variable that equals the result of your expression instead of the expression itself.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try this: (Note: This would be if you were using a shapefile and MP_VAL was the field you were pulling the 1603-01_mp2.0 value from.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lineName = '"Line" + !MP_VAL!.split("_")[0]'
arcpy.CalculateField_management(TempPLCL_Point, "Route", lineName, "PYTHON", "")&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Joel&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:18:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-arcgis-and-weird-string-error/m-p/429165#M33734</guid>
      <dc:creator>JoelCalhoun</dc:creator>
      <dc:date>2021-12-11T19:18:31Z</dc:date>
    </item>
    <item>
      <title>Re: Python, ArcGIS and Weird String Error</title>
      <link>https://community.esri.com/t5/python-questions/python-arcgis-and-weird-string-error/m-p/429166#M33735</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That pointed me in the right direction.&amp;nbsp; I ended up with:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;lineName = "'Line" + line + "'";&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Brad&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 30 Mar 2013 00:42:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-arcgis-and-weird-string-error/m-p/429166#M33735</guid>
      <dc:creator>BradOleson</dc:creator>
      <dc:date>2013-03-30T00:42:55Z</dc:date>
    </item>
  </channel>
</rss>

