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)
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.
1 Answer 1
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)
-
Thank you, Midavalo for you reply. i tried your suggestion but its not working. The field thickness accepts only ", " commas instead of "." decimal points( float nummer). The field calculator is greyed out. I think its a relational database table.Ram– Ram2017年04月28日 14:17:35 +00:00Commented Apr 28, 2017 at 14:17
-
Just curious as to how you know its a numeric field? I would have thought since it contained a comma that it would have to be text?GISHuman– GISHuman2017年04月28日 14:20:50 +00:00Commented Apr 28, 2017 at 14:20
-
1@GISKid The values are right-aligned in the screenshot. Numbers are (usually) right-aligned, text is left-aligned. I could be wrong though, other countries/regions may have the left/right align settings different than what I use2017年04月28日 14:21:26 +00:00Commented Apr 28, 2017 at 14:21
-
-
1yes exactly we use commas instead of decimal points in numeric fieldRam– Ram2017年04月28日 14:23:57 +00:00Commented Apr 28, 2017 at 14:23
print row[0]
to see what is actually being picked up by the cursor