AnsweredAssumed Answered

For Loop and Python String format()

Question asked by NatiNati1186 on Mar 25, 2020
Latest reply on Mar 26, 2020 by NatiNati1186

Hello,

 

I am trying to use UpdateCursor to update a field value by evaluating the value of another field. I have to do this in about 70 feature classes. For this reason I am using For Loop as well.

This is the script:

import arcpy

arcpy.env.workspace = r"D:\APRX_MXDS\USA_Parcels_2019_Project\Test.gdb"
arcpy.env.overwriteOutput = True

List_dissolve_fc = arcpy.ListFeatureClasses("*Dissolve")  # get a list of feature classes that end with "Dissolve"

fields = ["CO_NO", "COUNTY_NAME"]  # List of fields in test.gdb

for fc in List_dissolve_fc:  # Loop through each feature class in the list
    with arcpy.da.UpdateCursor(fc, fields) as cursor: # Loop through each feature
        for row in cursor:
            if (row[1] == "{}".format(fc[:-17])): # if COUNTY_NAME equals the name of the feature class without the last 17 characters, assign 74 to field CO_NO
                row[0] = 11
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 12
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 13
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 14
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 15
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 16
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 17
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 18
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 19
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 20
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 21
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 22
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 23
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 24
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 25
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 26
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 27
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 28
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 29
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 30
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 31
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 32
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 33
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 34
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 35
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 36
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 37
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 38
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 39
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 40
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 41
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 42
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 43
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 44
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 45
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 46
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 47
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 48
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 49
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 50
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 51
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 52
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 53
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 54
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 55
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 56
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 57
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 58
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 59
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 60
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 61
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 62
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 63
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 64
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 65
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 66
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 67
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 68
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 69
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 70
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 71
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 72
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 73
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 74
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 75
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 76
            elif (row[1] == "{}".format(fc[:-17])):
                row[0] = 77
            else:
                row[0] = None
                cursor.updateRow(row)

 

Expected behavior:

With the String format() method I am trying to do the following:

For every feature class whenever row[1] matches the name of the feature class minus the last 17 characters, assign the specified number to row[0]. I am expecting every feature class to only have one match and as a result one number will be assigned to row[0].

 

Actual Behavior:

I am getting the expected behavior but only in the last feature class on the list "List_dissolve_fc".

 

I know there is something very obvious to all of this but I cannot have a clear picture of what it is.

 

How could I solve this?

 

Thank you,

Natalia

Outcomes