I need to replace values in an attribute table for one column (replace zeroes in column named "label" to 100). Is this possible using ogr or python? I have to do this for 500+ shapefiles.
Asked
Active
Viewed 1,506 times
2
-
Yes you can easily do this in python. What did you try or where are you stuck ? You can start by having a look to the [`os.walk()`](https://docs.python.org/3/library/os.html#os.walk) python function (in order to traverse directory structure and get the name of your files) then you could give a try to the [`fiona`](https://github.com/Toblerity/Fiona) python module (it relies on OGR but offers an easier API, especially when you are only working with attributes). Depending on whether in a hurry you are to get the task done, you could also use the `threading` module in addition. – mgc Jan 15 '16 at 19:33
1 Answers
1
In the Esri ArcGIS realm, Update Cursors are typically used for this type of operation.
For example
import arcpy
# Your input feature class
fc = r'C:\path\to\your.gdb\feature_class'
# Start an update cursor and change values from 0 to 100 in a field called "your_field"
with arcpy.da.UpdateCursor(fc, "your_field") as cursor:
for row in cursor:
if row[0] == 0:
row[0] = 100
cursor.updateRow(row)

Borealis
- 8,044
- 17
- 64
- 112