Select to view content in your preferred language

Searching rows for values, appending fields based on those values.

975
2
03-16-2014 06:28 AM
AndrewTuleya
New Contributor
I have a table with one field called Cutwidth and Toothware. Cutwidth's values can either be 8, 12 or 16. Base on whatever value is in Cutwidth, I want my program to run a field calculator equation for the value of Toothware. Each equation is different based on the value of 8, 12 or 16. I currently am using a select by attribute tool and then filed calculator to do these steps, but instead of select by attribute, I would prefer to use something like a search cursor to read each value in a row and do something within that row based on that value. I am not getting the results I want w search cursor and was wondering if anyone had input for me to solve this issue?
Tags (2)
0 Kudos
2 Replies
FilipKrál
Frequent Contributor
Hi,
Search cursor is read only. You have to use update cursor like so:

in_fc = r'path/to/your/feature_class'

with arcpy.da.UpdateCursor(in_fc, ['Cutwidth', 'Toothware']) as uc:
    for row in uc:
        # with update cursor you can read data
        cutwidth = row[0]
        toothware = row[1]
        # but also update them (use whatever formulas you need)
        if cutwidth == 8:
            row[1] = cutwith + 10.0
        elif cutwidth == 12:
            row[1] = cutwith * cutwidth - 15.0
        elif cutwidth == 16:
            row[1] = cutwith * 0.5 + 20.0
        else:
            row[1] = -9999
    
        # don't forget to store your changes:
        uc.updateRow(row)


I recommend you look at description and examples in help too.
Hope this helps.
Filip.
0 Kudos
AndrewTuleya
New Contributor
Hey thanks for the input. I will give it a go tomorrow. I believe my problem was instead if using elif, I was just using if statements, and it would update all records in Toothware with the first true value it found for Cutwidth. Also, just by looking at it, this should work for any number of rows you have in a table correct? I will be importing new tables each week with varying number of records.
0 Kudos