1

I'm currently away in a remote field station using Arc 10.0 and having a hard time using an inline variable substitution for a numeric field value. I am trying to use UpdateCursor to select only a specific line by designating a "POSITION" field = to a numeric value that I can increment each time I go through the for loop. When I use a number in the line, it works. However, I can't seem to get the right syntax to substitute the hard-coded number for a variable.

here's the section of code:

## Attempt to update the cursor, then get value, then setvalue using an inline variable for the desired Field value
## THIS does not work
stefile = "ste1.shp"       
stefield = "POSITION"
steclus = "CLUSTER"
steac = "AC"
stevalue = 100
stepos = 22
Z = arcpy.UpdateCursor(stefile, '"POSITION" = stepos')        # This needs to be changed eg; '"POSITION" = a memvar') ...and at end of loop memvar +=1
for row in Z:
    print row.getValue(steclus)                       
    memvar = row.getValue(steclus)                   
    row.setValue(steac, stevalue)                   
    Z.updateRow(row)
    print row.getValue(steac)
    del Z
    del row

### HERE are the error messages
# Runtime error <type 'exceptions.RuntimeError'>: ERROR 999999:
# Error executing function.
# A column was specified that does not exist.

Am I simply using the wrong syntax? Or am I barking up the wrong tree? Guidance and advice welcome. Thanks.

  • 1
    As it is now, stepos is evaluated literally as stepos not the variable value 22. Try moving the variable outside the where clause string, like: ' "POSITION" = ' + stepos – phloem Nov 20 '14 at 19:18
  • Thanks phloem I hadn't tied that yet. Now the new error is "cannot concatenate 'str' and 'int' objects" – Stephen Morreale Nov 20 '14 at 19:39
  • Try: ' "POSITION" = ' + str(stepos) – phloem Nov 20 '14 at 19:40
  • Perfect solution. Thanks very much phloem. ~sjm – Stephen Morreale Nov 20 '14 at 19:51
1

The where clause is currently evaluating stepos literally as the value stepos, not the variable value 22.

Move the stepos variable outside the where clause string, casting the numeric value to a string, like:

arcpy.UpdateCursor(stefile, ' "POSITION" = ' + str(stepos))

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.