stringvariable = "banana"
arcpy.CalculateField_management("c:\point.shp", "SUBDIRECT", "stringvariable", "PYTHON")

I get an error saying stringvariable is not defined what's the deal? I check stringvariable by printing it to screen and it contains what it should. I am trying to use a string variable to calculate a field with arcpy. I am stumped. Also it works like this when run as a script through idle or outside of arcmap, but now I am trying to add this script to a toolbox and I am getting this error.


Should be:

stringvariable = "banana"
arcpy.CalculateField_management("c:\point.shp", "SUBDIRECT", "'" + stringvariable + "'", "PYTHON")

If you double quote stringvariable, Python won't interpret it as "banana". Also, You have to quote the string for the field calculator to work.

  • thank you. i found the answer and this was part of it the other part was that my variable contained slashes and underscores so I used: import re, stringvariable = re.escape(stringvariable) to translate it to string literal. – gomapping Mar 3 '14 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.