Select to view content in your preferred language

Correct Codeblock Python syntax for CalculateField

1899
11
Jump to solution
01-09-2023 08:12 AM
MPach
by
Frequent Contributor

 I'm using ArcMap 10.8.1 so this is Python 2.7.

  I'm trying to find the correct syntax to troubleshoot/debug this script that I'm working on. I was using the Python interpreter going line by line trying to resolve the issue and can't seem to figure out how to enter the codeblock into the interpreter. When I try to enter the codeblock as is I get a Syntax Error and I'm not skilled enough to know how to resolve it yet. It seems like the interpreter doesn't like the triple quotes for the codeblock. Any help is greatly appreciated!

 

 

expression = 'reclass(!{0}!,!{1}!,{2})'.format(lengthField, widthField, num)

codeblock = """ def reclass(lengthField,widthField,num):
                   if ((lengthField/num) >= (widthField)):
                       return 'True'
                   else:
                       return '' """

 

0 Kudos
11 Replies
Brian_Wilson
Honored Contributor

AHhhh, now that makes sense. Either "True" or "" goes into the attribute as its new value. Okay.

Passing Python code uninterpreted into a function like that creeps me out. 🙂 But that's me. I bet you can pass the strings to "exec" to test. After all, Esri must be doing something like that.

Yes so you can do this, but it runs fine, no errors.

Screenshot 2023-01-09 150110.png

MPach
by
Frequent Contributor

Also, I didn't want to return a 'False' string, I was just returning an empty string '', so there the field was left blank when it wasn't True. It just makes things visually easier when looking down the the column and I think it just looks cleaner that way. 

0 Kudos