Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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

# 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"
del rec

Good luck!

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
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) – Fezter Dec 25 '12 at 20:42
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. – Tomek Dec 27 '12 at 10:22

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("-", "")
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.