AnsweredAssumed Answered

Trying to calculate a text field in Python based on a variable, with no success

Question asked by bonfire98a on Jan 4, 2016
Latest reply on Jan 4, 2016 by bonfire98a

I have been pulling out my hair trying to perform what should be a very simple field calculation.  Below are code samples to show what works and what doesn't.


If I want to calculate "Main St and 1st Ave" into a text field, this works.

>>> arcpy.CalculateField_management(MyFC, "MyField", '"Main St and 1st Ave"')

<Result 'C:\\Test.gdb\\MyFC'>


Notice that the string is enclosed in double quotes, then further enclosed in single quotes.  It took me a while to track down that syntax quirk.


However, I am iterating through a cursor, and my input string will be different for each iteration.  I tried to set up the input for the calculatefield command accordingly.

>>> InputString = "'\"" + TextVar + "\"'"

>>> print InputString

'"Main St and 1st Ave"'


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?  But this is what I get instead.

>>> arcpy.CalculateField_management(MyFC, "MyField", InputString)

Traceback (most recent call last):

  File "<interactive input>", line 1, in <module>

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\", line 3354, in CalculateField

    raise e

ExecuteError: ERROR 999999: Error executing function.

Syntax error

Failed to execute (CalculateField).


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.  Your advice and comments are greatly appreciated.  Thank you.