0

I created new field in a table named 'Cimp'. Now I want to put there values that depends on values from column "COS_Cod" for example, if COS_Cod starts with 1.1 then Cimp is equal to 1. COS_Cod is a text, Cimp must be a number, because I will use it later for calculations.

I write code that base on code that I found in "Refining an Automated Model for Basic Landform Classification A Python and ArcGIS Approach" by Josh Moss. I doesn't give me errors, but when I check table my Cimp is unchanged.

This is my code:

#adding the value
tableFile = "C:/Users/User/Desktop/project/COS_CAOP/results/ClippedCOS.dbf"
arcpy.AddField_management(tableFile, "Cimp", "FLOAT","","","","","NULLABLE")
print ("TABLE DONE")


#Let's try this method!

COS_Cod = "COS_Cod"
Cimp = "Cimp"

with arcpy.da.UpdateCursor(pClippedCOS, (COS_Cod, Cimp)) as updateRows:
    for lForms in updateRows:
        cosCodes = lForms[0]
        print cosCodes

        print "printed cosCodes"

        def codeCimpAssign(COS_Cod):
            if cosCodes == "1.1*":
                numCimp = 1.0
            elif cosCodes == "1.2*":
                numCimp = 1.0
            elif cosCodes == "1.3*":
                numCimp = 0.8
            elif cosCodes == "1.4*":
                numCimp = 0.4
            elif cosCodes == "2.1*" and cosCodes == "2.2*":
                numCimp = 0.35
            elif cosCodes == "2.4*":
                numCimp = 0.35
            elif cosCodes == "3.1*":
                numCimp = 0.0
            elif cosCodes == "3.2*":
                numCimp = 0.25
            elif cosCodes == "3.3*":
                numCimp = 0.8
            else:
                numCimp = 0.0
                print "something"
            return numCimp


# define "text" as relationship for updating rows
        numCimpValue = codeCimpAssign(cosCodes)
        print numCimpValue
        print "printed Cimp values"

#update rows

        lForms[1] = numCimpValue
        updateRows.updateRow(lForms)
        print "Added CImp values"
        arcpy.AddMessage("Added CImp values")
1

There are some issues in your code. For example you cant use asterix to match beginnings of strings, instead use .startswith. You name input variable to function (COS_Cod) differently in the if statements (cosCodes), they need to be the same.

import arcpy

tableFile = "C:/Users/User/Desktop/project/COS_CAOP/results/ClippedCOS.dbf"

COS_Cod = "COS_Cod"
Cimp = "Cimp" #This is the field to update?


def codeCimpAssign(value):
    if value.startswith("1.1"):
        numCimp = 1.0
    elif value.startswith("1.2"):
        numCimp = 1.0
    #More elifs here...
    else:
        numCimp = 0.0
    return numCimp

with arcpy.da.UpdateCursor(tableFile,[COS_Cod, Cimp]) as cursor:
    for row in cursor:
        row[1]=codeCimpAssign(row[0])
        cursor.updateRow(row)
0

I manage to do it simply by changing cosCodes == "1.1*" so now this part looks like that:

    def codeCimpAssign(COS_Cod):
        if cosCodes[0:3] == "1.1":
            numCimp = 1.0
        elif cosCodes[0:3] == "1.2":
            numCimp = 1.0
            #rest of the code...

it worked and changed values in Cimp field correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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