<?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: Trying to calculate a text field in Python based on a variable, with no success in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360835#M28434</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Stephen,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I find it difficult to format text variables when quotations are involved.&amp;nbsp; One thing to try is to use the format option.&amp;nbsp; Personally, I think it makes formatting strings much easier.&amp;nbsp; For example, try the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;TextVar = "Main St and 1st Ave"
InputString = '"{0}"'.format(TextVar)

arcpy.CalculateField_management(myFC, "MyField", InputString)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 16:49:20 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2021-12-11T16:49:20Z</dc:date>
    <item>
      <title>Trying to calculate a text field in Python based on a variable, with no success</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360833#M28432</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have been pulling out my hair trying to perform what should be a very simple field calculation.&amp;nbsp; Below are code samples to show what works and what doesn't.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I want to calculate "Main St and 1st Ave" into a text field, this works.&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.CalculateField_management(MyFC, "MyField", '"Main St and 1st Ave"')&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;lt;Result 'C:\\Test.gdb\\MyFC'&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Notice that the string is enclosed in double quotes, then further enclosed in single quotes.&amp;nbsp; It took me a while to track down that syntax quirk.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I am iterating through a cursor, and my input string will be different for each iteration.&amp;nbsp; I tried to set up the input for the calculatefield command accordingly.&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;gt;&amp;gt;&amp;gt; InputString = "'\"" + TextVar + "\"'"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;gt;&amp;gt;&amp;gt; print InputString&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;'"Main St and 1st Ave"'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif;"&gt;Now, since the value of InputString is exactly the same as what I entered in the first calculatefield command, there shouldn't even be a question as to whether or not it would give the same result, no?&amp;nbsp; But this is what I get instead.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;gt;&amp;gt;&amp;gt; arcpy.CalculateField_management(MyFC, "MyField", InputString)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; File "&amp;lt;interactive input&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 3354, in CalculateField&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise e&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;ExecuteError: ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;Syntax error&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;Failed to execute (CalculateField).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hopefully it will turn out to be a very simple fix -- I would rather kick myself for not having seen something obvious than to continue to pull out my hair trying to figure out where I'm going wrong.&amp;nbsp; Your advice and comments are greatly appreciated.&amp;nbsp; Thank you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jan 2016 18:02:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360833#M28432</guid>
      <dc:creator>StephenRhone</dc:creator>
      <dc:date>2016-01-04T18:02:49Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to calculate a text field in Python based on a variable, with no success</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360834#M28433</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm curious why you wouldn't use an &lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/updatecursor-class.htm" title="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/updatecursor-class.htm"&gt;UpdateCursor—Help | ArcGIS for Desktop&lt;/A&gt; since you're already going through the records with a cursor. The syntax looks correct for your calculate field &lt;A href="http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/calculate-field.htm" title="http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/calculate-field.htm"&gt;Calculate Field—Help | ArcGIS for Desktop.&lt;/A&gt;​You&amp;nbsp; may want to try &lt;SPAN style="color: #4d4d4d; font-family: Consolas, 'Andale Mono', 'Lucida Console', Monaco, 'Courier New', Courier, monospace; font-size: 12px; background-color: #fafafa;"&gt;arcpy.env.overwriteOutput&lt;/SPAN&gt; = True. The data may be locked from the first calculate field.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jan 2016 18:29:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360834#M28433</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2016-01-04T18:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to calculate a text field in Python based on a variable, with no success</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360835#M28434</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Stephen,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I find it difficult to format text variables when quotations are involved.&amp;nbsp; One thing to try is to use the format option.&amp;nbsp; Personally, I think it makes formatting strings much easier.&amp;nbsp; For example, try the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;TextVar = "Main St and 1st Ave"
InputString = '"{0}"'.format(TextVar)

arcpy.CalculateField_management(myFC, "MyField", InputString)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:49:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360835#M28434</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T16:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to calculate a text field in Python based on a variable, with no success</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360836#M28435</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well, I'm not changing any of the data in the feature class upon which I was basing my cursor, so I don't really need an UpdateCursor for what I'm doing.&amp;nbsp; Thanks, though.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jan 2016 21:16:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360836#M28435</guid>
      <dc:creator>StephenRhone</dc:creator>
      <dc:date>2016-01-04T21:16:04Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to calculate a text field in Python based on a variable, with no success</title>
      <link>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360837#M28436</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Bingo!&amp;nbsp; That worked perfectly.&amp;nbsp; Thanks for passing that along.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jan 2016 21:16:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trying-to-calculate-a-text-field-in-python-based/m-p/360837#M28436</guid>
      <dc:creator>StephenRhone</dc:creator>
      <dc:date>2016-01-04T21:16:55Z</dc:date>
    </item>
  </channel>
</rss>

