[GIS] Replace field values with arcpy update cursor

arcpycursor

I am a bit new for the arcpy. I have a database table so I can't use field calculator. so I would like to use Python script for solving the issue.

import arcpy
... # Your input feature class
... table = r'D:\werk_map.gdb\test_gw_bsl'
... # Start an update cursor and change values from 0 to 100 in a field called "your_field"
... with arcpy.da.UpdateCursor(table, "Thickness") as cursor:
...     for row in cursor:
...         if row[0] == "0,1":
...             row[0] = "0,5"
...         cursor.updateRow(row)

enter image description here

I just want to replace all “0,1” values in the thickness field with “0,5”.
The above written arcpy script is running without error but not changing the values in the table.
I think may be it has do something with the comma in the field value.

Best Answer

First I don't see why you can't do this using the Field Calculator - it works on Database Tables as well as Feature Classes and Layers.

Now onto your script - you are very close. Your cursor if is checking for "0,1" and wanting to replace with "0,5" - the quote-marks around it means it is looking for text but your values are numbers, stored in a numeric type field. Python won't find text in a numeric field.

Also, because of how python treats commas, you will need to use a decimal point . in your code (it won't affect how it is stored in your table):

import arcpy
    # Your input feature class
    table = r'D:\werk_map.gdb\test_gw_bsl'
    # Start an update cursor and change values from 0 to 100 in a field called "your_field"
    with arcpy.da.UpdateCursor(table, "Thickness") as cursor:
        for row in cursor:
            if row[0] == 0.1:
                row[0] = 0.5
            cursor.updateRow(row)
Related Question