cursor.da.updateRow(row) Parameters

1116
2
11-15-2016 10:27 AM
HeatherWigell
New Contributor

Currently, I am attempting to write a script in #python 2.7.11 using arcpy.da.update.cursor  Essentially, I have an original script that works, which I need to rewrite and shorten. The original script is posted below.    The script is for a tool in ArcGIS used to calculate a desired number of Circuit ID's for the appropriate carrier.  Based on the input, the IDs are written back on to a table.

     Any help with the rewrite would be much appreciated. The question with this script is, should there be additional parameters in the line <cursor.da.updateRow(row)>.  The major issue, populating data back, seems to be with the with arcpy.da.cursor.update.  Additional info:  I wrote both of the scripts utilizing itertools (iterate), thinking I could replicate the pattern of the table quickly. The rest of the code, other than the update of the CircuitIDs, is identical. 

:
1) Attempt. Updated from last post. Uses itertools combination_with_replacement. itertools python doc for more info.
with arcpy.da.UpdateCursor(nodeFeatures, fields) as cursor:
    for row in cursor:
            def combinations_with_replacement(iterable, 10):
                if numIDs >= "1"
                and if numIDs <="10"
                    # combination_with_replacement(10,(1,'02')) --> 1,'03' 3,'05' 4,'06' 5,'07' 6,'04' 7,'05' 8,'06' 9,'07' 10,'08'
                    pool = tuple(iterable)
                    n = 1
                    r = 10
                    len(s)=75
                    range(i)==numIDs
                    cycles=range(n, n+1)
                    indices(i) = row[i+3] = ["(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (%02i)" %(i+1)
                    else:
                    return:

        elif numIDs =="0":
                row[3] = None
                row[4] = None
                row[5] = None
                row[6] = None
                row[7] = None
                row[8] = None
                row[9] = None
                row[10] = None
                row[11] = None
                row[12] = None

        else:
                cursor.updateRow(row):

2)The original code(My work only fills in for the section of "Update the appropriate number of Circuit ID fields"):
# ###################################################################################################
# Import system modules
# ###################################################################################################

import arcpy


# ###################################################################################################
# Set universal variables
# ###################################################################################################

nodeFeatures = r'OSP Mapping Layers\Node Locations'
numIDs = arcpy.GetParameterAsText(0)
fields = ['ExteNetNodeID', 'ProjectHub', 'ProjectCarrier', 'CircuitID1', 'CircuitID2', 'CircuitID3', 'CircuitID4', 'CircuitID5', 'CircuitID6', 'CircuitID7', 'CircuitID8', 'CircuitID9', 'CircuitID10']


# #########################
# Update the appropriate number of Circuit ID fields
# #########################

with arcpy.da.UpdateCursor(nodeFeatures, fields) as cursor:
    for row in cursor:
        if numIDs == "1":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            cursor.updateRow(row)
        elif numIDs == "2":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            cursor.updateRow(row)
        elif numIDs == "3":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            cursor.updateRow(row)
        elif numIDs == "4":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            cursor.updateRow(row)
        elif numIDs == "5":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            cursor.updateRow(row)
        elif numIDs == "6":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            cursor.updateRow(row)
        elif numIDs == "7":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)"
            cursor.updateRow(row)
        elif numIDs == "8":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)"
            row[10] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (08)"
            cursor.updateRow(row)
        elif numIDs == "9":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)"
            row[10] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (08)"
            row[11] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (09)"
            cursor.updateRow(row)
        elif numIDs == "10":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)"
            row[10] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (08)"
            row[11] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (09)"
            row[12] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (10)"
            cursor.updateRow(row)
        elif numIDs =="0":
            row[3] = None
            row[4] = None
            row[5] = None
            row[6] = None
            row[7] = None
            row[8] = None
            row[9] = None
            row[10] = None
            row[11] = None
            row[12] = None
            cursor.updateRow(row)
        else:
            pass

Like (0) Share
5 comments4 new updates
Heather Wigell
The question with this script is, should there be additional parameters in the line <cursor.updateRow(row)>.  The major issue, populating data back, seems to be with the with cursor.update.  Additional info:  I wrote both of the scripts utilizing itertools, thinking I could replicate the pattern of the table quickly. The rest of the code, other than the update of the CircuitIDs, is identical.   <a href>UpdateCursor—Help | ArcGIS for Desktop </>
0 Kudos
2 Replies
NeilAyres
MVP Alum

Firstly, please format you code correctly. Use the syntax highlighter to format to python. It will make it much easier to read and interpret.

You appear to be updating a bunch of text strings.

Have you considered using the format syntax?

This :

row[11] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (09)"

Could become something like :

row[11] = "({0}) - ({1}) - ({2}) - (09)".format(row[0], row[1], row[2])

But the whole process looks rather complex. Isn't there a better way.

I could not fail to notice that you make reference to OSP in your layer name.

Is that OSP Insight, the fibre management db?

BlakeTerhune
MVP Regular Contributor

As Neil mentioned, Posting code with Syntax Highlighting on GeoNet 

It looks like the numIDs parameter is supposed to be a number, so you should enforce that. Convert it to an integer so if it's not a number you'll get an error.

numIDs = int(arcpy.GetParameterAsText(0))‍‍‍‍‍‍‍

Alternatively, take a look at using arcpy.GetParameter() instead and specify the data type as integer when you create the script tool in ArcCatalog.

I haven't tested this code, but here's my take on how you could simplify your code so there's not so much repetition. With a lot of fields like this, I find it helpful to assign the indexes to a plain english variable name so the code is easier to read.

with arcpy.da.UpdateCursor(nodeFeatures, fields) as cursor:
    for row in cursor:
        ExteNetNodeID = row[0]
        ProjectHub = row[1]
        ProjectCarrier = row[2]
        id1 = row[3]
        id2 = row[4]
        id3 = row[5]
        id4 = row[6]
        id5 = row[7]
        id6 = row[8]
        id7 = row[9]
        id8 = row[10]
        id9 = row[11]
        id10 = row[12]
        if numIDs == 0:
            ## CircuitID fields start at index 3
            ## Total number of CircuitID fields is 10
            for i in range(3, 10+3):
                row[i] = None
        else:
            for i in range(3, numIDs+3):  ## CircuitID fields start at index 3
                row[i] = "({}) - ({}) - ({}) - ({})".format(
                    ExteNetNodeID,
                    ProjectHub,
                    ProjectCarrier,
                    "{:02d}".format(i-2)  ## pads with one leading zero to make two digits
                )
        cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

EDIT:

To answer your original question

The question with this script is, should there be additional parameters in the line <cursor.da.updateRow(row)

No, updateRow() only takes that one parameter as "a list or tuple of values. The order of values should be in the same order as the fields."

Python