1

I've been through multiple iterations / gyrations, and while there may be multiple problems here, the recurring issue is that the codeblock does not identify the existence of any syntax errors, but when it hits the first instance of drawing a value from a field, I get an error like "FACIL_ID" not defined.

Here's my most recent code:

import arcpy
from arcpy import env
env.workspace = "K:/Geobase/S-file/Floor Plans/GIS/SHP/Workspace"
expression1 = '"' + str(FACIL_ID) + "-" + str(SPACENAME) + "-001" + '"'
expression2 = '"' + str(FACIL_ID) + "-" + str(SPACENAME) + '"'
MyLength = len(str(SPACENAME))
if MyLength ==4:
  arcpy.CaluclateField_management("88888F10_SPACE_J2.shp","BSPACE_ID",expression1)
else:
  arcpy.CalculateField_management("88888F10_SPACE_J2.shp","BSPACE_ID",expression2)
4
  • 1
    The default is "VB", put in "PYTHON" at the end and try that: arcpy.CaluclateField_management("88888F10_SPACE_J2.shp","BSPACE_ID",expression1,"PYTHON") or change your plus signs to ampersands... note that Python is much preferable to VB as in some situations it will fail to operate even if the syntax is good. Also, your expression using an existing field should have exclamation marks around it : '"!' + str(FACIL_ID) + "!-" depending if FACIL_ID contains a valid field name. Commented Aug 12, 2014 at 4:11
  • 1
    please provide more code if you want more help. FACIL_ID seems to be a variable, but where does it come from ? I suggest that you print your expression to see what it returns. Also, you should not add extra '"' on each side of your expression, but you should write your field names between the appropriate characters (! ! in python, [ ] in VB) Commented Aug 12, 2014 at 9:42
  • 2
    You might find it a lot simpler to write and debug your expressions if you use str.format(), e.g., expression1 = '"{0}-{1}-001"'.format(FACIL_ID, SPACENAME) Commented Aug 12, 2014 at 12:59
  • CaluclateField is a misspelling. Commented Jan 5, 2019 at 4:19

1 Answer 1

4

This

expression1 = '"' + str(FACIL_ID) + "-" + str(SPACENAME) + "-001" + '"'

is trying to build a string out of two variables, FACIL_ID and SPACENAME. However, neither one has been defined (hence, the "not defined" error).

If they are supposed to be variables, they need to be defined before building a string, such as:

FACIL_ID = '88888F10'
SPACENAME = 'ABCDE'

If you want to calculate them from existing fields instead of setting variables within the script, the expression1 will need to be defined differently:

expression1 = '!FACIL_ID! + "-" + !SPACENAME! + "-001"'

If FACIL_ID is stored as an int instead of a string, then:

expression1 = 'str(!FACIL_ID!) + "-" + !SPACENAME! + "-001"'

Finally, make sure you indicate that you're using Python syntax for the Field Calculator expression (not VB):

arcpy.CalculateField_management("88888F10_SPACE_J2.shp","BSPACE_ID",expression1, "PYTHON")

Minor note:

if MyLength ==4:
  arcpy.CaluclateField_management...

Typo: should be CalculateField_management


If you want to be more advanced, using an UpdateCursor might be a faster way to set field values via ArcPy than Field Calculator.

2
  • I believe in a stand-alone py script you no longer use ! and instead use quotations to specify the field name? support.esri.com/en/knowledgebase/techarticles/detail/40355 Commented Aug 12, 2014 at 13:58
  • 1
    @GISKid -- I thought that was for a SQL query, not a Field Calculator expression Commented Aug 12, 2014 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.