Field Calc, in Model Builder, using strings

813
5
10-30-2019 12:38 PM
ChristopherSmith10
New Contributor II

Hello All, and thanks  for any help.

I am trying to take this block of Python and insert it into the Field Calculator.   What it is supposed to do is take a concatenated field (LF_Concat) and from within that field parse out the number of Greens (G), Yellows (Y), and Reds (R) and then apply a If/Than/elif to number of colors.

Right now I am at a loss on how to get it in.  I have this too show for it.  The parser while it shows VB is actually in Python  (I maybe slow, but I ain't that slow)

Progress Thwarted

I suspect that the delineatorList should be changed to the concatenated field (LF_concat)  but I am lost beyond that. 

Thanks,

Chris

delineatorList = [" ", ".", ","]

ColorClass =  ['GGGY','GGYY','GYYR']

for letters in ColorClass:
    #print letters, " G = ", letters.count('G')
    #print letters, " Y = ", letters.count('Y')
    #print letters, " R = ", letters.count('R')

    Count_G = letters.count('G')
    Count_Y = letters.count('Y')
    Count_R = letters.count('R')

    if Count_R > 0:
        print "Red"
    elif Count_R == 0 and Count_Y >= Count_G:
        print "Yellow"
    elif Count_R == 0 and Count_Y < Count_G:
        print "Green"

0 Kudos
5 Replies
FC_Basson
MVP Regular Contributor

First change your parser to Python.  The definition function declaration should be small caps - "def". And remember Python is strict on indentation.  You can also simplify your "checking" code to something like this:

for c in ColorClass:
    if c.count('R') > 0:
       print 'Red'
    elif c.count('R') == 0 and (c.count('Y') >= c.count('G')):
       print 'Yellow'
    elif c.count('R') == 0 and (c.count('Y') < c.count('G')):
       print 'Green'
DanPatterson_Retired
MVP Emeritus

The first line should be to make it a generic script

def Classify(ColorClass):

and you 'call' it as you have done.  The field name will be passed to the function as ColorClass so there is no need for you ColorClass = … assignment statement at all

And replace FC Basson print statement with return.

 (BTW, the print statement is python 2.7 if you are using ArcGIS Pro print('Red') would be needed)

ChristopherSmith10
New Contributor II

Expression:

Classify2(ColorClass)

Code Block:

def Classify2(ColorClass):
  for c in ColorClass:
    if c.count('R') > 0:
       return 'Red'
    elif c.count('R') == 0 and (c.count('Y') >= c.count('G')):
       return 'Yellow'
    elif c.count('R') == 0 and (c.count('Y') < c.count('G')):
       return 'Green'

Executing (Calculate Field): CalculateField WorkLF_P2 LanduseFeas Classify2(ColorClass) PYTHON_9.3 "def Classify2(ColorClass):\n for c in ColorClass:\n if c.count('R') > 0:\n return 'Red'\n elif c.count('R') == 0 and (c.count('Y') >= c.count('G')):\n return 'Yellow'\n elif c.count('R') == 0 and (c.count('Y') < c.count('G')):\n return 'Green'\n"

Start Time: Fri Nov 1 13:52:28 2019

ERROR 000539: Error running expression: Classify2(ColorClass)

Traceback (most recent call last):

File "<expression>", line 1, in <module>

NameError: name 'ColorClass' is not defined

Failed to execute (Calculate Field).

Failed at Fri Nov 1 13:52:28 2019 (Elapsed Time: 0.01 seconds)

So I got it into the code block and now I am attempting debugging. 

"ColorClass" is not defined, ...but it is the first line of the code

Thanks again.

0 Kudos
DanPatterson_Retired
MVP Emeritus

expression

Classify2(!LF_concat!)

ChristopherSmith10
New Contributor II

D'OH!

Thank you.  Works great now.

0 Kudos