Else if or maybe Where Clause for Update Cursor.

916
3
Jump to solution
04-25-2013 07:02 AM
ChristopherClark1
Occasional Contributor
I have several conditional statements I want to use to update values in an attribute table. I have been using update cursors as of late with some success. But now I am at a point that I am struggling with. I am able to use single argument conditions like

if row.Bkf_W <= 0:                 row.Width_Cat = 0                 rows.updateRow(row)             elif row.NEAR_DIST > 100:                 row.Snap_Code = 0                 rows.updateRow(row)


But what if I want to check a range of values. Like : Bkf_W > 0 and Bfw_W <=3 then Width_Cat = 1.
Would it be 0 < Bkf_W <= 3, then Width_Cat = 1? I have several of these "range" conditions (8 total) that I want to use to update cursors. Any help would be appreciated!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Pretty close, you just need one row reference though for your range. One other thing, you don't need to update your row after each if statement, you can just do it once at the end to save some lines. You also had an "O" in your first if statement instead of a zero.

Try this.
rows = arcpy.UpdateCursor(STHDLINES) for row in rows:  if row.Bkf_W <= 0:   row.Width_Cat = 0  elif 0 < Bkf_W <= 3:   row.Width_Cat = 1  elif 3 < Bkf_W <= 50:   row.Width_Cat = 2  elif row.Bkf_W > 50:   row.Width_Cat = 3  rows.updateRow(row)


You are most likely having issues with Notepad++ because it doesn't discriminate between tabs and spaces as well as other IDEs do.

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
Not sure what you are asking. Do you just need to figure out python syntax?  0 < Bkf_W <= 3 is perfectly valid, though your indentation is off in the code you posted.
0 Kudos
ChristopherClark1
Occasional Contributor
Sorry about the indentation. For some reason when I script things in Notepad++ then move it over to IDLE the indentations are always off. It looks good in notepad++ .

Yea, I was trying to figure out the syntax. So this will work:

rows = arcpy.UpdateCursor(STHDLINES)
for row in rows:
 if row.Bkf_W <= O:
  row.Width_Cat = 0
  rows.updateRow(row)
 elif row.Bkf_W 0 < Bkf_W <= 3:
  row.Width_Cat = 1
  rows.updateRow(row)
 elif row.Bkf_W 3 < Bkf_W <= 50:
  row.Width_Cat = 2
  rows.updateRow(row)
 elif row.Bkf_W > 50:
  row.Width_Cat = 3
  rows.updateRow(row)
0 Kudos
MathewCoyle
Frequent Contributor
Pretty close, you just need one row reference though for your range. One other thing, you don't need to update your row after each if statement, you can just do it once at the end to save some lines. You also had an "O" in your first if statement instead of a zero.

Try this.
rows = arcpy.UpdateCursor(STHDLINES) for row in rows:  if row.Bkf_W <= 0:   row.Width_Cat = 0  elif 0 < Bkf_W <= 3:   row.Width_Cat = 1  elif 3 < Bkf_W <= 50:   row.Width_Cat = 2  elif row.Bkf_W > 50:   row.Width_Cat = 3  rows.updateRow(row)


You are most likely having issues with Notepad++ because it doesn't discriminate between tabs and spaces as well as other IDEs do.
0 Kudos