0

I have records that need to be updated.
For this I select all records that are false and then process them with Calculate Field. The other records should not be changed. The output has NULL in each record. I don't understand where the error is. I also tried instead of return ags else: continue.

#select records for updating
arcpy.management.SelectLayerByAttribute("Strassenverzeichnis", "NEW_SELECTION", "AGS_NR_KOMMUNAL = '03154003' Or AGS_NR_KOMMUNAL = '03154010' Or AGS_NR_KOMMUNAL = '03154011' Or AGS_NR_KOMMUNAL = '03154020' Or AGS_NR_KOMMUNAL = '03154023' Or AGS_NR_KOMMUNAL = '03154401' Or AGS_NR_KOMMUNAL = '03156003' Or AGS_NR_KOMMUNAL = '03156012' Or AGS_NR_KOMMUNAL = '03156013' Or AGS_NR_KOMMUNAL = '03156016' Or AGS_NR_KOMMUNAL = '03156017' Or AGS_NR_KOMMUNAL = '03158015' Or AGS_NR_KOMMUNAL = '03158024' Or AGS_NR_KOMMUNAL = '03158026' Or AGS_NR_KOMMUNAL = '03158029' Or AGS_NR_KOMMUNAL = '15085010'", None)

#locale variables
inTable = "Straßenverzeichnis"
fieldName = "AGS_NR_KOMMUNAL"
expression = "upd(!AGS_NR_KOMMUNAL!)"
codeblock = """
def auf(liste):
    neu_liste = []
    for i in liste:
        neu_liste.append(upd(i))
    return neu_liste
    
def upd(ags):
    if ags == "03154003":
        return "03154028"
    elif ags ==  "03154010":
        return "03154028"
    elif ags ==  "03154011":
        return "03154027"
    elif ags ==  "03154020":
        return "03154027"
    elif ags ==  "03154023":
        return "03154027"
    elif ags ==  "03154401":
        return "03154016"
    elif ags ==  "03156003":
        return "03159004"
    elif ags ==  "03156012":
        return "03159036"
    elif ags ==  "03156013":
        return "03159036"
    elif ags ==  "03156016":
        return "03159036"
    elif ags ==  "03156017":
        return "03159002"
    elif ags ==  "03158015":
        return "03158039"
    elif ags == "03158024":
        return "03158040"
    elif ags ==  "03158026":
        return "03158039"
    elif ags ==  "03158029":
        return "03158040"
    elif ags ==  "15085010":
        return "15085330"
    return ags
"""
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3", codeblock)
5
  • before I just try to return the values. This also didnt work.
    – zyperngras
    Mar 15, 2021 at 16:00
  • I edit the code to an version without the replace method.
    – zyperngras
    Mar 15, 2021 at 16:05
  • 1
    One problem: the table name is spelled differently in the SelectLayerByAttribute command than it is in the inTable variable.
    – Dan C
    Mar 15, 2021 at 16:11
  • 2
    Also your auf() function appears not to be called by anything, make your code simpler, remove it.
    – Hornbydd
    Mar 15, 2021 at 16:46
  • Thank you a lot. It works now perfectly!
    – zyperngras
    Mar 16, 2021 at 8:21

1 Answer 1

1

This is code runs perfectly:

#select records for updating
arcpy.management.SelectLayerByAttribute("Strassenverzeichnis", "NEW_SELECTION", "AGS_NR_KOMMUNAL = '03154003' Or AGS_NR_KOMMUNAL = '03154010' Or AGS_NR_KOMMUNAL = '03154011' Or AGS_NR_KOMMUNAL = '03154020' Or AGS_NR_KOMMUNAL = '03154023' Or AGS_NR_KOMMUNAL = '03154401' Or AGS_NR_KOMMUNAL = '03156003' Or AGS_NR_KOMMUNAL = '03156012' Or AGS_NR_KOMMUNAL = '03156013' Or AGS_NR_KOMMUNAL = '03156016' Or AGS_NR_KOMMUNAL = '03156017' Or AGS_NR_KOMMUNAL = '03158015' Or AGS_NR_KOMMUNAL = '03158024' Or AGS_NR_KOMMUNAL = '03158026' Or AGS_NR_KOMMUNAL = '03158029' Or AGS_NR_KOMMUNAL = '15085010'", None)

#locale variables
inTable = "Strassenverzeichnis"
fieldName = "AGS_NR_KOMMUNAL"
expression = "upd(!AGS_NR_KOMMUNAL!)"
codeblock = """
def upd(ags):
    if ags == "03154003":
        return "03154028"
    elif ags ==  "03154010":
        return "03154028"
    elif ags ==  "03154011":
        return "03154027"
    elif ags ==  "03154020":
        return "03154027"
    elif ags ==  "03154023":
        return "03154027"
    elif ags ==  "03154401":
        return "03154016"
    elif ags ==  "03156003":
        return "03159004"
    elif ags ==  "03156012":
        return "03159036"
    elif ags ==  "03156013":
        return "03159036"
    elif ags ==  "03156016":
        return "03159036"
    elif ags ==  "03156017":
        return "03159002"
    elif ags ==  "03158015":
        return "03158039"
    elif ags == "03158024":
        return "03158040"
    elif ags ==  "03158026":
        return "03158039"
    elif ags ==  "03158029":
        return "03158040"
    elif ags ==  "15085010":
        return "15085330"
    return ags
"""
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3", 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.