Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"
share|improve this question
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 Feb 16 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 Feb 16 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 Feb 16 at 14:23
    
@dru87 the da cursors require 10.1+. 10.0 didn't have them. – nmpeterson Feb 16 at 15:31
    
@nmpeterson, sorry 10.x was a bit unclear. I did mean anything higher than 10.0. – dru87 Feb 16 at 16:21
up vote 6 down vote accepted

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.

share|improve this answer
    
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 Feb 16 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. – John Reiser Feb 16 at 14:12
    
That is helpful! thank you! – student Feb 16 at 14:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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