2

I would like to update a field with new values. With the code below it assigns the value '2' to all the rows and not just if the field == 2. But by 'print i' and 'print "it is 2"' everything looks correct. Any ideas?

    field = "field"
    field2List = []
    rows = arcpy.SearchCursor(input_layer)
    for my_row in rows:
        field2List.append(my_row.getValue(field))

    cursor = arcpy.UpdateCursor(input_layer)
    for row in cursor:
        for i in field2List:
            print i
            if i == 2:
                print "it is 2"
                row.setValue(my_field,2)
                cursor.updateRow(row)
            else:
                print "not 2"
5
  • 1
    What version of ArcGIS are you using? In case it's 10.x you could use the much faster arcpy.da.UpdateCursor(). Note that it has a slightly different syntax.
    – dru87
    Commented Feb 16, 2016 at 13:52
  • I use 10.2.2 and it didn't work to run the arcpy.da.UpdateCursor() so I thought it is only for the ArcGIS pro
    – student
    Commented Feb 16, 2016 at 14:08
  • 1
    It should work. The underlined arguments are required: arcpy.UpdateCursor(dataset, where-clause, spatial-reference, fields, sortfield), whereas arpy.da.UpdateCursor(intable, fieldnames, where-clause, spatial-reference, explodetopoint, sql-clause). I use ArcGIS 10.3 Advanced and the da.UpdateCursor() works flawlessly.
    – dru87
    Commented Feb 16, 2016 at 14:23
  • @dru87 the da cursors require 10.1+. 10.0 didn't have them.
    – nmpeterson
    Commented Feb 16, 2016 at 15:31
  • @nmpeterson, sorry 10.x was a bit unclear. I did mean anything higher than 10.0.
    – dru87
    Commented Feb 16, 2016 at 16:21

1 Answer 1

6

If you are looking to update a specific field, for example - population, if that same field is a certain value, then specify the where clause in the UpdateCursor.

cursor = arcpy.UpdateCursor(input_layer, "POPULATION", "POPULATION < 10000")

will only update the records where the value in the population field is less than 10,000. The second argument to the UpdateCursor is the list of field names and the third argument is the SQL where clause.

2
  • I am probably not allowed to do that. But how would you rewrite the condition if I need to use a regular expression? I have to assign dates to the seasons. For example, for date in datevalueList: match1 = re.search('[0-9][0-9][0-9][0-9]-12-[0-9][0-9]\s[0][0]:[0][0]:[0][0]', date) condition = ?? cursor = arcpy.UpdateCursor(input_layer, condition)
    – student
    Commented Feb 16, 2016 at 14:04
  • 2
    Don't use regular expressions to parse dates, as dates can be formatted in a multitude of ways. If your field is already a date type, use Python's datetime module to parse out the month, then a basic case statement to select seasons. Again, your seasons will be different based on hemisphere and local culture. Commented Feb 16, 2016 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.