2

I'm trying to calculate a field in Model Builder using the Calculate Field tool. I'm using the following code block and expression in the calculation, but am getting an "invalid syntax (, line 1)" error.

# Code Block:
def get_block(block_id):
 block_only = str(block_id)[1:]
 if block_only[-1].isalpha():
 return block_only.zfill(3)
 else:
 return block_only.zfill(2)
# Expression:
get_block(%block_id%)

%block_id% is an in-line substitution that points to a string variable I create as the output of a custom Python script tool. Any ideas what the error is?

asked Mar 11, 2013 at 21:30
0

1 Answer 1

2

You need to quote the in-line substitution to pass the string variable.

The expression should read:

 get_block('%block_id%')

or

 get_block("%block_id%")

See the caution note in this help topic: http://resources.arcgis.com/en/help/main/10.1/index.html#//004000000004000000

answered Mar 11, 2013 at 22:04
2
  • Ah, yes, that must be it. Will verify tomorrow when I'm back at work. Thanks! Commented Mar 11, 2013 at 22:35
  • Just made the change and it worked perfectly. Thank you, again. Commented Mar 12, 2013 at 13:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.