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)