6

I would like to find a Python script that will access an attribute table within a shapefile and either update or replace records inside a field column. More specifically, I would like to use the replace expression to convert a table record like "123-456-789" to "123456789". I have many records to process and would like the best approach to do this task using Python.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Dec 24, 2012 at 22:43

4 Answers 4

13

What you need is an UpdateCursor.

The UpdateCursor function creates a cursor that lets you update or delete rows on the specified feature class, shapefile, or table. The cursor places a lock on the data that will remain until either the script completes or the update cursor object is deleted.

Your code would look something like the following:

import arcpy
# Create update cursor for feature class 
# 
rows = arcpy.UpdateCursor("path_to_your_data_here") 
for row in rows:
 # Fields from the table can be dynamically accessed from the row object.
 # Here, the field is named targetField as I don't know your field name
 targetRow = row.targetField #Assigns value of targetField to string
 row.targetField = targetRow.translate(None, '-') #Removes the dashes
 rows.updateRow(row) 
# Delete cursor and row objects to remove locks on the data 
# 
del row 
del rows

If you don't have ArcPy, you can use dbfpy to directly access and edit the attributes in the shape file's dbf file. The syntax is rather simple:

from dbfpy import dbf
db = dbf.Dbf("your_file.dbf")
#Editing a value, assuming you want to edit the first field of the first record
rec = db[0]
rec["FIRST_FIELD"] = "New value"
rec.store()
del rec
db.close()
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Dec 25, 2012 at 3:16
5

Alternatively and very straight forward you could also open the dbase table (dbf) coming with your shape in excel or libreoffice calc. They should be full editable and therefore you can simply remove all dashes in multiple columns.

answered Dec 25, 2012 at 11:17
1
  • DBF tables cannot be edited Commented Jan 22, 2022 at 21:20
3

You may also use Field Calculator with Python expression !Field!.replace('-','').

urcm
22.6k4 gold badges59 silver badges109 bronze badges
answered Dec 25, 2012 at 12:05
2
  • Field calculator would work well, but I don't think it's as fast as an update cursor. And the translate method is faster than replace (link) Commented Dec 25, 2012 at 20:42
  • 1
    OP say that he have many records to convert, but does not specify if he needs to do more than one .shp file or even fields, so simplest answer is to use Field Calculator rather than writing script with update cursor. Its good to know that translate would work faster than replace method. Commented Dec 27, 2012 at 10:22
1

I would use a data access module da Update Cursor and the replace() method to accomplish this. The following is the preferred (new) method of using an Update Cursor.

import arcpy
shp = r'C:\path\to\shapefile.shp'
with arcpy.da.UpdateCursor(shp, "your_field") as cursor:
 for row in cursor:
 row[0].replace("-", "")
 cursor.updateRow(row)
answered Feb 3, 2016 at 13:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.