5

I've hit a wall trying to loop through multiple fields with arcpy.UpdateCursor. I have a list of files (2) with multiple fields (10-15'ish) that have its own error check in SQL, and an error value to be written. I am wanting to loop through and update each field with its programmed value.

    import arcpy
    FILE = 'X:\\SOME_FILE'
    FIELDS = [ "FIELD_X", "...", "FIELD_N"]
    SQL = "SOME_FIELD = SOME_VALUE" # or less than, greater, etc
    for FIELD in FIELDS:
        rows = arcpy.UpdateCursor(FILE, SQL, "", FIELD)
        row.FIELD = 'FIELD_ERROR_VALUE'
        rows.updateRow(row)
    del rows

There's a bit more to it, but the short is that row.FIELD is read as row.'FIELD_NAME', with FIELD_NAME as a string value. I'm rather new to the backside of Python programming and got thrown in just to learn. I've worked through many issues, but this one has got me stumped. If row.FIELD would work, it would be easier than going through each single field with it's NAME, SQL, UPDATE_VALUE, etc manually. It works if I type in row.WHATEVER_FIELD = ..., but calling the variable (from string) adds a ' to both ends as a string value. The only thing not working in the loop is row.FIELD.

I'm working with python2.6, arc10.0, mostly win7.

Any help would be greatly appreciated.

GM

4
  • I'm still a bit unclear about what you're trying to accomplish. In your script you're inserting the same string 'FIELD_ERROR_VALUE' into every field. Your SQL statement is static as well, although you commented it may also be less than or greater than, etc.
    – Roy
    Aug 8, 2012 at 11:57
  • @Roy - Each field has it's own sql that is found in a list. Each is static. I was trying to keep things short so someone (PolyGeo) could glance and say, "row.setValue()" without wading through 300 lines of code and a error from the shell.
    – gm70560
    Aug 8, 2012 at 13:28
  • minor thing, and it is probably right in your script... but you are not looping through the rows in your update cursor. You are calling "row.FIELD" without defining row as a result. Aug 10, 2012 at 13:16
  • Oops, you're right
    – gm70560
    Aug 12, 2012 at 17:49

2 Answers 2

8

I think you need to use row.setValue(FIELD)

3
  • row.FIELD = "someString" is functionally the same
    – Roy
    Aug 8, 2012 at 11:52
  • 1
    I don't think that row.FIELD will work with field being the name of a variable. Aug 8, 2012 at 13:13
  • Ahhh I see your point!
    – Roy
    Aug 8, 2012 at 14:04
1

I think this will work better:

    import arcpy
    FILE = 'X:\\SOME_FILE'
    FIELDS = [ "FIELD_X", "...", "FIELD_N"]
    rows = arcpy.UpdateCursor(FILE)
    for row in rows:
         for FIELD in FIELDS:
              row.setValue(FIELD, "FIELD_ERROR_VALUE")
              rows.updateRow(row)

This will allow you to loop through the table 1 time and the field list multiple times.

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.