Bugs in script to populate an empty attribute field

2569
5
08-16-2013 02:36 PM
by Anonymous User
Not applicable
Original User: sellingfloe

Here is a copy of my script.

[ATTACH=CONFIG]26766[/ATTACH]

I am attempting to populate a blank field (Veg_Height) in my shapefile attribute table with text of my own choosing depending on the value in field Class_name.

e.g. if Class_name = "Grass", change the blank Veg_Height field to "Short", if Class_name = "Trees", change the blank Veg_Height field to "Tall" etc.

Unfortunately, at the moment, I keep getting errors when I try to run this in model builder. Can anyone suggest any changes I could make?

Thanks 🙂
0 Kudos
5 Replies
AmyKlug
Occasional Contributor III
Here is a copy of my script.

[ATTACH=CONFIG]26766[/ATTACH]

I am attempting to populate a blank field (Veg_Height) in my shapefile attribute table with text of my own choosing depending on the value in field Class_name.

e.g. if Class_name = "Grass", change the blank Veg_Height field to "Short", if Class_name = "Trees", change the blank Veg_Height field to "Tall" etc.

Unfortunately, at the moment, I keep getting errors when I try to run this in model builder. Can anyone suggest any changes I could make?

Thanks 🙂



I also might try writing your if statement like this:


if row[0] == "Trees":
    row[1] = "Tall"



EDIT: Deleted first comment
0 Kudos
by Anonymous User
Not applicable
Original User: msayler

The '=' is used for assignment. Since you're doing a comparison, you need to use the 'equal to' operator instead, '=='.

i.e.
if row[0] == "Grass":
    row[1] = "Short"
0 Kudos
by Anonymous User
Not applicable
Original User: mzcoyle

As Amy and Matt pointed out, you are using the wrong equal operator for conditionals. Another option is to use a dictionary which can remove the need for conditionals completely, as well as making your code more easily extensible if you plan on adding additional classifications in the future.

curs_dict = {
    "Grass": "Short",
    "Trees": "Tall",
    "Urban": "N/A"}

for row in cursor:
    row[1] = curs_dict[row[0]]
    cursor.updateRow(row)
0 Kudos
KerryThomas
New Contributor II
Thanks so much!
0 Kudos
by Anonymous User
Not applicable
Original User: curtvprice

Here is a copy of my script.


[thread=48475]How to post Python code[/thread]
0 Kudos