2

Before I begin I should emphasise that my python knowledge is pretty limited; generally self taught or inspired by snippets I have found on here.

I have a .dbf table, and I need to use python to increment values (row above + 1) in a field ONLY when a value in another field is 0. For all other rows the value is 1. To better illustrate this, when it is complete the table should look something like this:

FID   Value   Increment
 0      1        0
 1      1        0
 2      1        0
 3      1        0
 4      0        1
 5      0        2
 6      0        3
 7      0        4
 8      1        4
 9      1        4
 10     1        4
 11     1        4
 12     0        5

Using the code from the top answer to Computing new attribute based on changes in another attribute using ArcGIS Desktop with Python?

I edited the code to look like this:

import arcpy

fc = arcpy.GetParameterAsText(0)

rows = arcpy.UpdateCursor(fc)

increment = 0
for row in rows:
    if row.VALUE == 0:
        increment += 1
        row.INCREMENT = increment
        rows.updateRow(row)

    while row.VALUE == 1:
        row.INCREMENT = increment
        rows.updateRow(row)
        rows.next()

    row.INCREMENT = increment
    rows.updateRow(row)

del row, rows

I run it, and it takes quite a while (I have about 11,000 records in the table), before I get this error:

Traceback (most recent call last):
  File "C:\scratch\incrementLine_IncreFieldEverytimeIncrementChanges.py", line 16, in <module>
    rows.updateRow(row)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 102, in updateRow
    return convertArcObjectToPythonObject(self._arc_object.UpdateRow(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.

The thing is though, the code DOES work, it gives me the result in the table that I want, but due to this error I can't slot this script into my ModelBuilder flow, which is rather frustrating!

So my question is, what is causing this error? As I said, the script is editing the table how I want it, so if there is way I can just stop the error so I can use it in a flow that would be ideal! I imagine my limited knowledge in python (and scripting in general) is making me miss something entirely obvious, and also the fact that I'm basically ripping off a script from 2012 could be introducing out of date techniques?

  • I have just run the process again, but converted my .dbf table to a geodatabase table for input, and at present it is still running, and has been for 10 minutes (the error came up after about a minute before). If this works after some time I shall let you know! (Though I wasn't expecting it have to process for this long, it could just be hanging) Also, thank you @PolyGeo for the tags and tidying, appreciated! – Smiff Lord Feb 13 '17 at 13:51
2

You could try this:

import arcpy

fc = arcpy.GetParameterAsText(0)

rows = arcpy.UpdateCursor(fc)

increment = 0
for row in rows:
    if row.VALUE == 0:
        increment += 1

    row.INCREMENT = increment
    rows.updateRow(row)

del row, rows
  • Excellent! That has worked perfectly and is quick! Thank you very much @faith_dur , this has solved my problem, so will be accepting it! I take it that the whole 'while' section of the script was redundant? Always nice to learn through doing things like this too – Smiff Lord Feb 13 '17 at 15:47

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.