Select to view content in your preferred language

Python Update Cursor tool produces infinite loop in ArcMap

374
1
03-26-2012 11:29 AM
johnodonnell
Deactivated User
Hello. I am very new to Python and scripting in general, and would appreciate any assistance with my following problem.

I've created an extremely simple Python script that strips away unnecessary characters from fields in a shapefile. The script works within PythonWin and returns no errors. However, when I make a script tool in ArcMap and try to deploy the same function, it crashes ArcMap. Sometimes the data is actually updated, other times no. But every time I try the script tool, ArcMap becomes unresponsive, jumps to 100% of CPU use, and has to be shut down from within Task Manager. Below is my working script. Any comments or suggestions would be welcome.

Thank you for your time.

import arcpy

featureClass = arcpy.GetParameterAsText(0)
rows = arcpy.UpdateCursor(featureClass)
row = rows.next()

while row:
    subtypeValue = row.SUBTYPE
    subtypeValueIndex = subtypeValue.split(" ")
    row.SUBTYPE = subtypeValueIndex[0]

    materialValue = row.STRMAT
    strmatValueIndex = materialValue.split("-")
    row.STRMAT = strmatValueIndex[0]

    photoynValue = row.PHOTOYN
    photoynValueIndex = photoynValue.split("-")
    row.PHOTOYN = photoynValueIndex[0]

    conditionValue = row.CONDITION
    conditionValueIndex = conditionValue.split("-")
    row.CONDITION = conditionValueIndex[0]

    clidmatValue = row.CLIDMAT
    clidmatValueIndex = clidmatValue.split("-")
    row.CLIDMAT = clidmatValueIndex[0]

    accesstypeValue = row.ACCESSTYPE
    accesstypeValueIndex = accesstypeValue.split("-")
    row.ACCESSTYPE = accesstypeValueIndex[0]

    groundtypeValue = row.GROUNDTYPE
    groundtypeValueIndex = groundtypeValue.split("-")
    row.GROUNDTYPE = groundtypeValueIndex[0]

    stepsValue = row.STEPS
    stepsValueIndex = stepsValue.split("-")
    row.STEPS = stepsValueIndex[0]

    skimmerValue = row.SKIMMER
    skimmerValueIndex = skimmerValue.split("-")
    row.SKIMMER = skimmerValueIndex[0]

    lcyclestatValue = row.LCYCLESTAT
    lcyclestatValueIndex = lcyclestatValue.split("-")
    row.LCYCLESTAT = lcyclestatValueIndex[0]

    collectbyValue = row.COLLECTBY
    collectbyValueIndex = collectbyValue.split("-")
    row.COLLECTBY = collectbyValueIndex[0]

    datasourceValue = row.DATASOURCE
    datasourceValueIndex = datasourceValue.split("-")
    row.DATASOURCE = datasourceValueIndex[0]

    erosionValue = row.EROSION
    erosionValueIndex = erosionValue.split("-")
    row.EROSION = erosionValueIndex[0]

   
   
    rows.updateRow(row)
    row = rows.next()

del row, rows
Tags (2)
0 Kudos
1 Reply
KimOllivier
Honored Contributor
Because you did not surround your script with CODE tags I cannot see if your indenting was correct.
You could also use a slightly more compact for-loop to avoid forgetting the next() at the end of the steps.
I have changed 'rows' to 'cur' so that it is not confused with 'row'
You may be able to use the strip() function instead of split() if all you are doing is removing surrounding whitespace. This is more general than splitting by a space.

import arcpy

featureClass = arcpy.GetParameterAsText(0)
cur = arcpy.UpdateCursor(featureClass)
for row in cur:
    subtypeValue = row.SUBTYPE
    subtypeValueIndex = subtypeValue.split(" ")
    row.SUBTYPE = subtypeValueIndex[0]
    materialValue = row.STRMAT
    strmatValueIndex = materialValue.split("-")
    row.STRMAT = strmatValueIndex[0]
    photoynValue = row.PHOTOYN
    photoynValueIndex = photoynValue.split("-")
    row.PHOTOYN = photoynValueIndex[0]
    conditionValue = row.CONDITION
    conditionValueIndex = conditionValue.split("-")
    row.CONDITION = conditionValueIndex[0]
    clidmatValue = row.CLIDMAT
    clidmatValueIndex = clidmatValue.split("-")
    row.CLIDMAT = clidmatValueIndex[0]
    accesstypeValue = row.ACCESSTYPE
    accesstypeValueIndex = accesstypeValue.split("-")
    row.ACCESSTYPE = accesstypeValueIndex[0]
    groundtypeValue = row.GROUNDTYPE
    groundtypeValueIndex = groundtypeValue.split("-")
    row.GROUNDTYPE = groundtypeValueIndex[0]
    stepsValue = row.STEPS
    stepsValueIndex = stepsValue.split("-")
    row.STEPS = stepsValueIndex[0]
    skimmerValue = row.SKIMMER
    skimmerValueIndex = skimmerValue.split("-")
    row.SKIMMER = skimmerValueIndex[0]
    lcyclestatValue = row.LCYCLESTAT
    lcyclestatValueIndex = lcyclestatValue.split("-")
    row.LCYCLESTAT = lcyclestatValueIndex[0]
    collectbyValue = row.COLLECTBY
    collectbyValueIndex = collectbyValue.split("-")
    row.COLLECTBY = collectbyValueIndex[0]
    datasourceValue = row.DATASOURCE
    datasourceValueIndex = datasourceValue.split("-")
    row.DATASOURCE = datasourceValueIndex[0]
    erosionValue = row.EROSION
    erosionValueIndex = erosionValue.split("-")
    row.EROSION = erosionValueIndex[0]
    cur.updateRow(row)


del row, rows 
0 Kudos