2

I have a arcpy.script that calculates another field based on an existing field. However I only want the field to populate a value if the existing field is a specific value. This would leave some field values null which is what I need in the end. My problem is the code seems to ignore the if and elif statements and calculates every field regardless of its value. Here is the portion of code that is giving me trouble.

#Calculate field based on SH field
fc = "w2p"

cursor =arcpy.UpdateCursor(fc)

for row in cursor:
    if row.SH == 1:
        arcpy.CalculateField_management("w2p", "SH_SLANT", "!slant!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_CELLAR", "!CELLAR!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "CASING", "!SURF_CAS!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD83Y", "!NAD83_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD83X", "!NAD83_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD27Y", "!NAD27_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD27X", "!NAD27_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD83Y", "!a1819_JFW_BH_NAD83_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD83X", "!a1819_JFW_BH_NAD83_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD27Y", "!a1819_JFW_BH_NAD27_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD27X", "!a1819_JFW_BH_NAD27_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_ELEV", "!ELEV!", "PYTHON_9.3")
    elif row.SH == 2:
        arcpy.CalculateField_management("w2p", "SH_SLANT2", "!slant!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_CELLAR2", "!CELLAR!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "CASING2", "!SURF_CAS!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD83Y2", "!NAD83_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD83X2", "!NAD83_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD27Y2", "!NAD27_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD27X2", "!NAD27_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD83Y2", "!a1819_JFW_BH_NAD83_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD83X2", "!a1819_JFW_BH_NAD83_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD27Y2", "!a1819_JFW_BH_NAD27_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD27X2", "!a1819_JFW_BH_NAD27_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_ELEV2", "!ELEV!", "PYTHON_9.3")
    elif row.SH== 3:
        arcpy.CalculateField_management("w2p", "SH_SLANT3", "!slant!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_CELLAR3", "!CELLAR!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "CASING3", "!SURF_CAS!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD83Y3", "!NAD83_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD83X3", "!NAD83_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD27Y3", "!NAD27_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD27X3", "!NAD27_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD83Y3", "!a1819_JFW_BH_NAD83_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD83X3", "!a1819_JFW_BH_NAD83_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD27Y3", "!a1819_JFW_BH_NAD27_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD27X3", "!a1819_JFW_BH_NAD27_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_ELEV3", "!ELEV!", "PYTHON_9.3")
    elif row.SH == 4:
        arcpy.CalculateField_management("w2p", "SH_SLANT4", "!slant!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_CELLAR4", "!CELLAR!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "CASING4", "!SURF_CAS!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD83Y4", "!NAD83_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD83X4", "!NAD83_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD27Y4", "!NAD27_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_NAD27X4", "!NAD27_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD83Y4", "!a1819_JFW_BH_NAD83_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD83X4", "!a1819_JFW_BH_NAD83_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD27Y4", "!a1819_JFW_BH_NAD27_Y!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "BH_NAD27X4", "!a1819_JFW_BH_NAD27_X!", "PYTHON_9.3")
        arcpy.CalculateField_management("w2p", "SH_ELEV4", "!ELEV!", "PYTHON_9.3")
1
  • You either want to use an update cursor in which you access and update the fields, OR Calculate Field outside of any kind of loop. Currently you're re-running each Calculate Field once for every row, but all of them apply to the entire table regardless – mikewatt Sep 12 '18 at 17:02
4

Unless you have a very old version of ArcGIS (<10.1) there is no reason not to use the newer data access cursors, for example da.UpdateCursor:

UpdateCursor establishes read-write access to records returned from a feature class or table.

They are a lot faster. The da.UpdateCursor can read and update records so no need for field calculator.

I think your problem can be solved with code below. I have no data to try it on though:

import arcpy
featureclass = r'C:\data.gdb\w2p' #Change to match your feature class

valuefields = ['slant','CELLAR','SURF_CAS','NAD83_Y','NAD83_X',
               'NAD27_Y','NAD27_X','a1819_JFW_BH_NAD83_Y','a1819_JFW_BH_NAD83_X',
               'a1819_JFW_BH_NAD27_Y','a1819_JFW_BH_NAD27_X','ELEV']

updatefields = ['SH_SLANT','SH_CELLAR','CASING','SH_NAD83Y','SH_NAD83X',
                'SH_NAD27Y', 'SH_NAD27X','BH_NAD83Y','BH_NAD83X',
                'BH_NAD27Y', 'BH_NAD27X', 'SH_ELEV']

for n in ['1','2','3','4']:
    sql = """{0}={1}""".format(arcpy.AddFieldDelimiters(featureclass,'SH'),n)
    if n != '1':
        updatefields2 = [f+n for f in updatefields] #Add 2,3 or 4 to each field in updatefield list
    else:
        updatefields2 = updatefields #Dont add 1 
    with arcpy.da.UpdateCursor(featureclass,updatefields2+valuefields,where_clause=sql) as cursor:
        for row in cursor:
            for i in range(0,11):
                row[i]=row[i+12] #First field equals the 13th field, second = 14th field, and so on
            cursor.updateRow(row)

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.