1

I created a model that runs, but want it as a script. I exported it and made some small changes, but now I'm getting an error in the Field Calculation. Excerpt from the code: I created a field:

arcpy.AddField_management(Final, "mobCode", "TEXT", "", "", "12", "", "NULLABLE", "NON_REQUIRED", "")

Then, I calculate the field "mobCode":

arcpy.CalculateField_management(final_shp, "mobCode", "mob(!gridcode!, !soil_code!, !mobCode!)", "PYTHON_9.3", "def mob(fld1, fld2, mobCode):
    if fld1 >0 and fld1 <=15 and fld2 == \"Good\":
        return \"Go\"
    elif fld1 >15 and fld1 <45 and fld2 == \"Good\":
        return \"Slow Go\"
    elif fld1 >0 and fld1 <=15 and fld2 == \"Fair\":
        return \"Slow Go\"
    elif fld1 >15 and fld1 <45 and fld2 == \"Fair\":
        return \"No Go\"
    elif fld1 >45:
        return \"No Go\"
    else:
        return \"No Cal\"")

I get an EOL string literal error at the arcpy.CalculateField_management line behind the colon.

I've referenced this page, but...not quite getting it: Error calculating field with script exported from ModelBuilder

I also read that it's better to develop the codeblock as its own variable (maybe codeblock)?

0

2 Answers 2

1

Use da.UpdateCursor instead, see the first example in the documentation:

import arcpy

fc = 'c:/data/base.gdb/well'
fields = ['WELL_YIELD', 'WELL_CLASS']

# Create update cursor for feature class 
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    # For each row, evaluate the WELL_YIELD value (index position 
    # of 0), and update WELL_CLASS (index position of 1)
    for row in cursor:
        if (row[0] >= 0 and row[0] <= 10):
            row[1] = 1
        elif (row[0] > 10 and row[0] <= 20):
            row[1] = 2
        elif (row[0] > 20 and row[0] <= 30):
            row[1] = 3
        elif (row[0] > 30):
            row[1] = 4
    # Update the cursor with the updated list
    cursor.updateRow(row)
0

If you don't want to use a cursor as @BERA suggested then I suggest you review the Help file of the Field calculate tool and study the code samples as they will show you best practise.

You can make your code more understandable and debug it if you separate out the code block as shown in the Help file.

Text values can be enclosed in single quotes.

So I would rewrite, using the advice in the Help file your code to:

import arcpy

codeblock = """def mob(fld1, fld2, mobCode):
    if fld1 >0 and fld1 <=15 and fld2 == 'Good':
        return 'Go'
    elif fld1 >15 and fld1 <45 and fld2 == 'Good':
        return 'Slow Go'
    elif fld1 >0 and fld1 <=15 and fld2 == 'Fair':
        return 'Slow Go
    elif fld1 >15 and fld1 <45 and fld2 == 'Fair':
        return 'No Go'
    elif fld1 >45:
        return 'No Go'
    else:
        return 'No Cal'"""

arcpy.CalculateField_management(final_shp, "mobCode", "mob(!gridcode!, !soil_code!, !mobCode!)", "PYTHON_9.3", codeblock)

Your Answer

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

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