Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This should be simple but I've spent days trying everything to get it to work. I need to update a field in a layer based on two other fields. I can't even get it to update based on one field. Could someone please tell me whats wrong. So in this script I want to be classifying the outfield as "poor" if the infield is "Sandy". I will also have another field of numbers (and infield2 = "10").

rows = arcpy.UpdateCursor(infc) #this is my feature layer
for row in rows:
    if row.getValue(Infield1) == "Sandy":
        row.setValue(Outfield, "poor")
    else:
        row.setValue(Outfield, "none")
    rows.updateRow(row)
del rows

Thanks for all the answers. The script only works if written like this: I did not want to name the field names though, but at least it works.

The only way I could do a range 5 - 10 was to put > 5 and <10 (see line 23)

for row in rows:
    if row.getValue("Texture") == "Sandy" and row.getValue("Humus") < 10:
        row.setValue("Richness", "poor")
    elif row.getValue("Texture") == "Sandy" and row.getValue("Humus") > 10:
        row.setValue("Richness", "medium")
    elif row.getValue("Texture") == "Sandy-Clay" and row.getValue("Humus") < 5:
        row.setValue("Richness", "poor")
    elif row.getValue("Texture") == "Sandy-Clay" and row.getValue("Humus")  >= 5 and row.getValue("Humus")  <= 10:
        row.setValue("Richness", "medium")
    elif row.getValue("Texture") == "Sandy-Clay" and row.getValue("Humus") > 10:
        row.setValue("Richness", "rich")
    elif row.getValue("Texture") == "Clay" and row.getValue("Humus") < 5:
        row.setValue("Richness", "poor")
    elif row.getValue("Texture") == "Clay" and row.getValue("Humus") > 5:
        row.setValue("Richness", "rich")
    else:
        row.setValue("Richness", "unclassified")
    rows.updateRow(row)
del rows, row
share|improve this question

4 Answers 4

up vote 3 down vote accepted

Try removing the parentheses from your "if" statements so that you have:

if row.getValue("Infield1") == "Sandy":
share|improve this answer
    
thanks! thats why it wouldnt work. –  Nevu Sep 27 '11 at 14:44
    
Glad I could help out! @Hairy posted some good alternatives using the "row.fieldname" method instead of the "row.getValue()" and "row.setValue()" which I find a little more straightforward when working with fixed field names. PS - don't forget to upvote us and accept an answer! :) –  Jason Sep 27 '11 at 14:58

You can always use "with" that allows objects like files/cursores to be used in a way that ensures they are always cleaned up promptly and correctly. But it needs the table/fc field names to be passed as arguments as well.

fields = ('InField1', 'Outfield')
    try:
        with arcpy.da.UpdateCursor(infc,fields) as cursor:
           for row in cursor:
               if row[0]== "Sandy":         
                   row[1] = "poor"    
               else:         
                   row[1] = "none"    
               cursor.updateRow(row)
    except Exception, e:
        # If an error occurred, print line number and error message
        tb = sys.exc_info()[2]
        print "Line %i" % tb.tb_lineno
        print e.message
share|improve this answer
    
You're mixing and matching search with update cursors. You're also mixing and matching the data access module with the normal cursor. You need to pass field names differently. –  Paul Aug 13 '13 at 23:53
    
@Paul, thank you for poiting it out! i've edited the code to use normal module instead of the da module, like he used to. I forgot da module requires fields as argument as well! –  neverMind Aug 14 '13 at 4:28
    
Not to nitpick, but normal cursors (as far as I know) do not support with statements. At least in 10.1 SP1 I have never been able to get them to work. –  Paul Aug 14 '13 at 4:31
    
Ok, I thought i've used it before, but I obviously confused. –  neverMind Aug 14 '13 at 4:36
    cur = arcpy.UpdateCursor(infc, "Where Infield1 = 'Sandy'") #this is my feature layer
    for row in cur:
        row.Outfield = "poor"
        cur.updateRow(row)
        del row
    del cur

    cur = arcpy.UpdateCursor(infc, "Where Infield1 <> 'poor'") #this is my feature layer
    for row in cur:
        row.Outfield = "none"
        cur.updateRow(row)
        del row
    del cur

or

cur = arcpy.UpdateCursor(infc) 
for row in cur:
    if row.Infield1 == "Sandy":         
        row.Outfield = "poor"    
    else:         
        row.Outfield = "none"    
    cur.updateRow(row) 
    del row
del cur

Is how I would do this. Too many times I have had issues trying to retrieve values with getValue(fieldname) so I reference the columns themselves as row.columnName and it works fine

Hope this helps.

share|improve this answer
    
thanks. It seems my script doesn't work with Infield1 etc instead of the actual name of the field. –  Nevu Sep 27 '11 at 14:57
1  
I believe that in the first instance which uses a "where" statement to return specific rows, you need to have quotations marks "" around the field name in the case of shapefiles and brackets [] for geodatabase feature classes. –  Jason Sep 27 '11 at 15:05
    
ahh! I had it working after removing the brackets and changing the field names to the actual field name. But now it doesn't seem to work. I also tried it with the cur method and nothing happens even though it says completed what have I done wrong..I tried with [] instead as well rows = arcpy.UpdateCursor(infc) for row in rows: if row.getValue("Texture") == "Sandy": row.setValue("Richness", "poor") else: row.setValue("Richness", "none") rows.updateRow(row) del rows –  Nevu Sep 27 '11 at 16:01
    
Could you edit your original question with the new pieces of code you've been trying and having trouble with? It's difficult to piece together what you've done and where things might be going wrong. –  Jason Sep 27 '11 at 17:21

Change

del rows

to

del rows, row
share|improve this answer
1  
thanks, but it works with either del rows, row or del rows –  Nevu Sep 27 '11 at 14:45

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.