mercredi 29 janvier 2020

Trying to use CalculateField in Python IDE to populate new field in geodatabase based on values in existing field

I am struggling with some code that I am writing to update a geodatabase feature class called "Grabs_WCVI". I have a new field called "SubModCat" that I want to populate with a ranking system using an if elif else statement based on values in an existing field called "BType". The values for BType can range from 1/1a/1b/2/2a/2b/3/3a/3b. I haven't used codeblocks before and I'm finding that the IDE accepts my local variables, but won't run my CalculateField function after, so there must be an error in there...

Here is my code so far:

# Set environment settings
arcpy.env.workspace = "C:\Users\gillespiek\Grabs_DataCleaning\All_BoP_Grabs_only.gdb"

# Set local variables
inTable = "Grabs_WCVI" 
fieldName = "SubModCat"
expression = "newValue(!Btype!)"
codeblock = """def newValue(field):
    val1 = [‘1’, ‘1a’, ‘1b’]
    val2 = [‘2’, ‘2a’, ‘2b’]    
    if field in val1:
        return 1
    elif field in val2:
        return 2
    elif field == ‘3a’:
        return 3
    elif field == ‘3b’:
        return 4
    else:
        return 0""" 

# Execute CalculateField
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON_9.3",
                                codeblock)

Can anyone see where I'm going astray? Any advice is greatly appreciated :

Aucun commentaire:

Enregistrer un commentaire