Select to view content in your preferred language

Convert dbf to numpy array

2516
3
12-15-2011 01:02 PM
AlexanderKeyel
Deactivated User
I need to use Python to convert a data table in .dbf format generated by ArcGIS 10 to a Numpy array.

Is there a simple way to do this?  Forgive me if this is a naive question, but I was unable to find anything on the help pages.

Thanks!
Tags (2)
0 Kudos
3 Replies
ChrisSnyder
Honored Contributor
I don't have an example, but basically you would have to use a search cursor to loop through the fields and records.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001q000000
0 Kudos
AlexanderKeyel
Deactivated User
Thanks!  I'm still pretty new to Python, so never would have found that.
Below is the code that I came up with:

from numpy import *
import arcpy

rows = arcpy.SearchCursor("Z://Code//EX2_near.dbf","","","","")

currentIN_FID = ""
currentNear_FID = ""
currentNear_Dist = ""
newdata = []

# Iterate through the rows in the cursor
#
for row in rows:
    rowN = [row.IN_FID, row.NEAR_FID, row.NEAR_DIST]
    newdata.append (rowN)

print newdata
datatwo = array([newdata])
0 Kudos
ChrisSnyder
Honored Contributor
Yep -

Also, if you wanted to "variabalize" the field names (use the field names as varaibles) it would look like:

from numpy import *
import arcpy
rows = arcpy.SearchCursor("Z://Code//EX2_near.dbf","","","","")
currentIN_FID = "IN_FID" 
currentNear_FID = "NEAR_FID"
currentNear_Dist = "NEAR_DIST"
newdata = []
# Iterate through the rows in the cursor 
for row in rows: 
   rowN = [row.getValue(currentIN_FID), row.getValue(currentNear_FID), row.getValue(currentNear_Dist)]
   newdata.append (rowN)
del row, rows #be sure to delete the cursor objects, otherwise a lock on the table will persist!!!
print newdata
datatwo = array([newdata]) 
0 Kudos