1

I'm trying to use a codeblock, that I've used in the field calculator of a shapefile before, in an ArcPy script with UpdateCursor.

The code I've used in the field calculator:

Pre-Logic Script Code:

def Reclass(NFK1M):
    if (NFK1M <= 50):
        return 10
    elif (NFK1M >50 and NFK1M <= 90):
        return 50
    elif (NFK1M > 90 and NFK1M <= 140):
        return 125
    elif (NFK1M> 140 and NFK1M <= 200):
        return 250
    elif (NFK1M > 200 and NFK1M <= 250):
        return 500
    elif (NFK1M > 250):
        return 750


Pktzl_B =
Reclass(!NFK1M!)

This works just fine. I've now tried to incorporate this in an arcpy-script with first creating the field "Pktz_B" and then filling it with UpdateCursor, but it always returns "750" from the last elif statement:

def punktzahl_b(fieldname):
    if 'NFK1M' <= 50:
        return 10
    elif 'NFK1M' > 50 and 'NFK1M' <= 90:
        return 50
    elif 'NFK1M' > 90 and 'NFK1M' <= 140:
        return 125
    elif 'NFK1M' > 140 and row1[0] <= 200:
        return 250
    elif 'NFK1M' > 200 and 'NFK1M' <= 250:
        return 500
    elif 'NFK1M' > 250:
        return 750


for fc in arcpy.ListFeatureClasses():
    if field_exists(fc, 'NFK1M'):
        arcpy.AddField_management(fc, 'Pktz_B', 'short')
        with arcpy.da.UpdateCursor(fc, 'Pktz_B') as cursor:
            for row in cursor:
                row[0] = punktzahl_b('NFK1M')
                cursor.updateRow(row)

I've also tried it like this, and it always returns "10" from the first if statement:

def punktzahl_b(fieldname):
    if row[0] <= 50:
        return 10
    elif row[0] > 50 and row[0] <= 90:
        return 50
    elif row[0] > 90 and row[0] <= 140:
        return 125
    elif row[0] > 140 and row[0] <= 200:
        return 250
    elif row[0] > 200 and row[0] <= 250:
        return 500
    elif row[0] > 250:
        return 750


for fc in arcpy.ListFeatureClasses():
    if field_exists(fc, 'NFK1M'):
        arcpy.AddField_management(fc, 'Pktz_B', 'short')
        with arcpy.da.UpdateCursor(fc, 'Pktz_B') as cursor:
            for row in cursor:
                row[0] = punktzahl_b('NFK1M')
                cursor.updateRow(row)
0

1 Answer 1

0

You need to include 'NFJK1M' field in the cursor and use it as input to the function:

...
for fc in arcpy.ListFeatureClasses():
    if field_exists(fc, 'NFK1M'):
        arcpy.AddField_management(fc, 'Pktz_B', 'short')
        with arcpy.da.UpdateCursor(fc, ['Pktz_B','NFK1M']) as cursor:
            for row in cursor:
                row[0] = punktzahl_b(row[1]) #here, row[1] is the NFK1M value
                cursor.updateRow(row)
1
  • 1
    That's it, thank you! For anyone wondering, in the "def punktzahl_b(fieldname):" the if-statements need to be checking row[1], too. Commented Jul 9, 2021 at 7:35

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.